Validating the presence of foreign keys /associated objects

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) 2)

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


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?


class Fit < ActiveRecord::Base belongs_to :Part belongs_to :Brand

It probably doesn't make a difference, but conventionally the attribute ids should be lowercase

     belongs_to :part      belongs_to :brand

validates_presence_of :part_id

The association name is :part not :part_id so

    validates_presence_of :part