开发者

Is there a better way to validate associated record in rails3

开发者 https://www.devze.com 2023-01-29 07:09 出处:网络
I am using rails3 and I have following code class Utensil < ActiveRecord::Base validates_presence_of :manufac开发者_如何学Cturer_id

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

精彩评论

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