开发者

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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜