Andrew France
2009-Aug-10 03:40 UTC
Call AR belongs_to association accessor ID method on object assignment
Hi, Haven''t been able to find this raised before but I''m sure it must have been. When assigning an object to a belongs_to association I would like it to call the *association*_id= method instead of directly updating the attributes array. This is because I want to modify the behaviour when assigning a particular object without writing separate methods for whether it''s just an ID being assigned or a full object. Example: a = Author.find(2) b = Book.new b.author = a # Should call b.author_id=(val) Is there already a simple way to achieve this? (without having to run validation) Changing it would be a one-line patch, but can anyone think of problems this would introduce? Thanks, Andrew
Frederick Cheung
2009-Aug-10 07:29 UTC
Re: Call AR belongs_to association accessor ID method on object assignment
On Aug 10, 4:40 am, Andrew France <andrew+li...-nb9sXpffzH/10XsdtD+oqA@public.gmane.org> wrote:> Hi, > > Haven''t been able to find this raised before but I''m sure it must have > been. When assigning an object to a belongs_to association I would > like it to call the *association*_id= method instead of directly > updating the attributes array. This is because I want to modify the > behaviour when assigning a particular object without writing separate > methods for whether it''s just an ID being assigned or a full object. >There''s a ticket on lighthouse about this, I seem to recall there''s a patch too. Fred> Example: > a = Author.find(2) > b = Book.new > b.author = a # Should call b.author_id=(val) > > Is there already a simple way to achieve this? (without having to run > validation) > > Changing it would be a one-line patch, but can anyone think of > problems this would introduce? > > Thanks, > Andrew