validates_associated ... doesn''t

Fraser Campbell
Wed, 26 Oct 2005 17:15:47 -0000

Hi,

I think I''m missing some fundamental information here so hopefully
someone can help me.  I have a model Club which has many members,
members of course belong to a Club.  I''d like to ensure that when a
members is saved (saved/created/updated) only valid club_id''s are
accepted.

I have a Club model (simplified) like this:

   class Club < ActiveRecord::Base
     has_many :members
   end

I have a Member model (simplified) like this:

   class Member < ActiveRecord::Base
     belongs_to :club
     validates_associated :club
     validates_presence_of :club_id
   end

All other validations (not shown) and basic CRUD works as expected.
However, when I insert a junk club_id it is accepted and saved in the
database.

Am I doing something wrong?  I am using rails release 0.13.1-1 on Ubuntu
Linux.

Thx,
Fraser