Is there a better way to validate associated record in rails3
I am using rails3 and I have following code
class Utensil < ActiveRecord::Base
validates_presence_of :manufac开发者_如何学Cturer_id
belongs_to :manufacturer
end
Is that the right way to validate a belongs_to object. I have a feeling that validating id might not be the best strategy. Any alternative solution.
What you have is just fine. If you want to validate the associated model as well, you can fo the following:
class Utensil < ActiveRecord::Base
belongs_to :manufacturer
validates_associated :manufacturer
validates_presence_of :manufacturer_id
end
Hope this helps!
精彩评论