hi guys, I been trying to find out on how to validate the presence of foreign keys/associated objects and the threads that talk about this are rather dated and unanswered. I looked at 1) http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/f8175db2a8321750/d565496a3672f19f?lnk=gst&q=validates+presence+foreign#d565496a3672f19f 2) http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/cff66eead0e5580b/c032b19f1832b2e0?lnk=gst&q=validates+presence+foreign#c032b19f1832b2e0 Consider this: class Part < ActiveRecord::Base belongs_to :brand belongs_to :category belongs_to :sub_category belongs_to :user belongs_to :status has_many :fits has_many :images, :dependent => :destroy validates_presence_of :title, :description, :category, :location, :contact_name, :contact_number, :condition end class Brand < ActiveRecord::Base has_many :designs validates_presence_of :name end class Fit < ActiveRecord::Base belongs_to :Part belongs_to :Brand validates_presence_of :part_id end When I hit the "submit" button to purposely try and create a new "Fit" object without selected a Brand (the brand is a drop down box) and the Part ID is a hidden field, no errors come up. How do I validate the presence of the "Part ID" (or part object) within the Fit model? I tried putting " validates_presence_of :part_id" and it does not work. Any ideas, guys? thanks -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rick DeNatale
2010-May-06 17:23 UTC
Re: Validating the presence of foreign keys /associated objects
On Thu, May 6, 2010 at 10:20 AM, ct9a <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class Fit < ActiveRecord::Base > belongs_to :Part > belongs_to :BrandIt probably doesn''t make a difference, but conventionally the attribute ids should be lowercase belongs_to :part belongs_to :brand> > validates_presence_of :part_idThe association name is :part not :part_id so validates_presence_of :part HTH -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.