# Call AR belongs\_to association accessor ID method on object assignment

**URL:** <https://www.ruby-forum.com/t/call-ar-belongs-to-association-accessor-id-method-on-object-assignment/173470>\
**Category:** Rails\
**Created:** [August 10, 2009, 5:41am UTC](https://www.ruby-forum.com/t/call-ar-belongs-to-association-accessor-id-method-on-object-assignment/173470 "2009-08-10T05:41:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [August 10, 2009, 5:41am UTC](https://www.ruby-forum.com/t/call-ar-belongs-to-association-accessor-id-method-on-object-assignment/173470/1 "2009-08-10T05:41:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [August 10, 2009, 9:30am UTC](https://www.ruby-forum.com/t/call-ar-belongs-to-association-accessor-id-method-on-object-assignment/173470/2 "2009-08-10T09:30:40Z")

</div>

On Aug 10, 4:40 am, Andrew F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
