You can explicitly get the object that you want from the Customerorder.user = customer.__getobj__
But it probably depends on what else you may be doing with the customer object and that's almost the same as your user_id = id example.You could also override the user= method to assign the id from whatever object you're handing to itdef user=(user)self.user_id = user.idend
For situations similar to this I'm using a gem I wrote that applies methods from a module http://github.com/saturnflyer/castingBut for that you'd really need Ruby 2.x (though it would work in 1.9) and you'd need to accept that method_missing would be implemented on your user class. I tried to make the readme informative if you'd like to check it out.There's also http://github.com/steveklabnik/becoming that relies on the same behavior in Ruby 2.x
When wrapping an object using SimpleDelegator, instead of extending it using a Module, the class changes which causes issues with ActiveRecord assosiations which are type checked, giving `ActiveRecord::AssociationTypeMismatch`.
customer = Customer.new(user)order.user = customerorder.save #=> ActiveRecord::AssociationTypeMismatch (Got Customer, Expected User)I could do `order.user_id = customer.id` but it doesn't look so nice.Does anyone know of a nice way of getting around this?--Write intention revealing code #=> http://www.clean-ruby.com
--
You received this message because you are subscribed to the Google Groups "Objects on Rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objects-on-rai...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
You can explicitly get the object that you want from the Customer
order.user = customer.__getobj__
def class
__getobj__.class
end
end
but get the rarther odd error: User(#2154403100) expected, got
User(#2154403100).
But it probably depends on what else you may be doing with the customer object and that's almost the same as your user_id = id example.
You could also override the user= method to assign the id from whatever object you're handing to it
def user=(user)self.user_id = user.idend
For situations similar to this I'm using a gem I wrote that applies methods from a module http://github.com/saturnflyer/castingBut for that you'd really need Ruby 2.x (though it would work in 1.9) and you'd need to accept that method_missing would be implemented on your user class. I tried to make the readme informative if you'd like to check it out.
There's also http://github.com/steveklabnik/becoming that relies on the same behavior in Ruby 2.x
When wrapping an object using SimpleDelegator, instead of extending it using a Module, the class changes which causes issues with ActiveRecord assosiations which are type checked, giving `ActiveRecord::AssociationTypeMismatch`.
customer = Customer.new(user)order.user = customerorder.save #=> ActiveRecord::AssociationTypeMismatch (Got Customer, Expected User)
I could do `order.user_id = customer.id` but it doesn't look so nice.
Does anyone know of a nice way of getting around this?
--Write intention revealing code #=> http://www.clean-ruby.com
--
You received this message because you are subscribed to the Google Groups "Objects on Rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objects-on-rai...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Objects on Rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objects-on-rai...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.