Re: validates_associated & foreign keys

Frederick Cheung
Tue, 01 Jul 2008 13:35:34 -0000

On 1 Jul 2008, at 14:27, Birgit Arkesteijn wrote:

>
> Hi Frederick,
>
> Thanks for your message.
> I changed my ''belongs_to'' and
''has_many'' and (indeed) the problem
> persists.
>
> I know that RoR doesn''t really know about foreign keys, but what
is
> the
> purpose of validates_associated ??
>
http://ar.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000090
>
It runs the validations on the association. If there is no associated
model (eg because the foreign key is dangling) it doesn''t
care
> What kind of associating does it perform, it not on the values between
> two models?
>
> Maybe I should rephrase my question ...
>
> If validates_associated doesn''t do the trick, how do I write a
> validation that checks that area_codes_market.area_code_id ==
> area_codes.id
>

 From the api docs:

NOTE: This validation will not fail if the association hasn''t been
assigned. If you want to ensure that the association is both present
and guaranteed to be valid, you also need to use validates_presence_of.

Fred