# Assigning to the foreign key on a belongs\_to association

**URL:** <https://www.ruby-forum.com/t/assigning-to-the-foreign-key-on-a-belongs-to-association/136876>\
**Category:** Rails\
**Created:** [May 7, 2008, 2:23am UTC](https://www.ruby-forum.com/t/assigning-to-the-foreign-key-on-a-belongs-to-association/136876 "2008-05-07T02:23:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Westcott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Matt\_Westcott](https://www.ruby-forum.com/u/Matt_Westcott)\
**Post date:** [May 7, 2008, 2:23am UTC](https://www.ruby-forum.com/t/assigning-to-the-foreign-key-on-a-belongs-to-association/136876/1 "2008-05-07T02:23:56Z")

</div>

I’ve encountered what seems like an odd omission in the behaviour of  
belongs\_to associations: if you assign to the foreign key attribute,  
it doesn’t update the associated object (or mark a previously loaded  
one as stale) until you explicitly save or reload it.

Let’s say we have a Company model, which belongs\_to :city :

> > torchbox = Company.find\_by\_name(‘Torchbox’)  
> > =\> #\<Company id: 1, name: “Torchbox”, city\_id: 1\>
> 
> > torchbox.city  
> > =\> #\<City id: 1, name: “Oxford”\>
> 
> > torchbox.city\_id = 2  
> > =\> 2
> 
> > torchbox.city  
> > =\> #\<City id: 1, name: “Oxford”\>

(Would have expected City id: 2 to be returned here…)

I’ve come up with a 6-line patch (see below) and accompanying plugin  
to fix this, but I get the feeling that for something so elementary to  
have gone unfixed for so long, there must be some perceived good  
reason why it works the way it does. If so, what’s the reason? And if  
not, is this a patch that’s worth pushing through the official  
channels?

- Matt

diff --git a/activerecord/lib/active\_record/associations.rb b/  
activerecord/lib/active\_record/associations.rb  
index 0809b27…7135e50 100644  
— a/activerecord/lib/active\_record/associations.rb  
+++ b/activerecord/lib/active\_record/associations.rb  
@@ -1094,6 +1094,13 @@ module ActiveRecord  
instance\_variable\_set(ivar, new\_value.nil? ? nil :  
association)  
end

- 

```
     if association_proxy_class == BelongsToAssociation

```

- 

```
       define_method("#{reflection.primary_key_name}=") do |

```

target\_id|

- 

```
         instance_variable_set(ivar, nil)

```

- 

```
         self["#{reflection.primary_key_name}"] = target_id

```

- 

```
       end

```

- 

```
     end

```

- 

```
     define_method("set_#{reflection.name}_target") do |

```

target| return if target.nil? and association\_proxy\_class  
== BelongsToAssociat association =  
association\_proxy\_class.new(self, reflection)  
diff --git a/activerecord/test/cases/associations/  
belongs\_to\_associations\_test.rb b/activerecord/test/cases/associations/  
belongs\_to\_associations\_test.rb  
index b8ec911…c130db2 100644— a/activerecord/test/cases/  
associations/belongs\_to\_associations\_test.rb  
+++ b/activerecord/test/cases/associations/  
belongs\_to\_associations\_test.rb  
@@ -45,6 +45,13 @@ class BelongsToAssociationsTest \<  
ActiveRecord::TestCase  
assert\_equal apple.id, citibank.firm\_id  
end

- def test\_foreign\_key\_assignment
- signals37 = accounts(:signals37)
- assert\_equal companies(:first\_firm), signals37.firm
- signals37.firm\_id = companies(:another\_firm).id
- assert\_equal companies(:another\_firm), signals37.firm
- end
- def test\_no\_unexpected\_aliasing  
first\_firm = companies(:first\_firm) another\_firm =  
companies(:another\_firm)

---

<div class="post-metadata">

**Author:** ![Matt\_Westcott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Matt\_Westcott](https://www.ruby-forum.com/u/Matt_Westcott)\
**Post date:** [May 7, 2008, 2:56am UTC](https://www.ruby-forum.com/t/assigning-to-the-foreign-key-on-a-belongs-to-association/136876/2 "2008-05-07T02:56:40Z")

</div>

rails-core is the place to ask this sort of thing!

Fred
