On Mon, 2011-02-07 at 14:28 +0100, Rainer Frey wrote: > On Mon, Feb 7, 2011 at 11:58 AM, Xavier Noria <[email protected]> wrote: > > On Sun, Feb 6, 2011 at 2:10 PM, Rainer Frey <[email protected]> wrote: > > > >> Unfortunately the Rails Guide on Active Record Validation and > >> Callbacks says (Section 3.9): > >> "If you want to be sure that an association is present, you’ll need to > >> test whether the foreign key used to map the association is present, > >> and not the associated object itself." > >> > >> Maybe the guide needs to be updated. > > > > Yeah, I think the wording is unfortunate. Certainly you can't be sure > > the association is present by checking that the FK attribute is > > present. > > > > Rather, this topic deserves a warning in my view. Something in the > > line that if you check whether the FK attribute is present then you > > *don't know* whether it is valid. You can decide to take the risk, > > that's up to you, but the reader should be warned. > > But this thread seems to suggest one should simply validate the > association attribute instead. Is that not sufficient then?
I'd say validating the association attribute would be the best practice in 3.1, but it may result in an extra query to the database to fetch the associated record, if it's not loaded or if it's stale. If users wish to avoid that overhead, they can check the FK, but should be aware that this does not guarantee the associated record actually exists. -- http://jonathanleighton.com/
signature.asc
Description: This is a digitally signed message part
