# Has\_many with different foreign key

**URL:** <https://www.ruby-forum.com/t/has-many-with-different-foreign-key/196890>\
**Category:** Rails\
**Created:** [January 31, 2009, 12:52pm UTC](https://www.ruby-forum.com/t/has-many-with-different-foreign-key/196890 "2009-01-31T12:52:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sSshartles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@sSshartles](https://www.ruby-forum.com/u/sSshartles)\
**Post date:** [January 31, 2009, 12:52pm UTC](https://www.ruby-forum.com/t/has-many-with-different-foreign-key/196890/1 "2009-01-31T12:52:10Z")

</div>

Hello,

I am new to rails and am trying to get my associations straight. I  
have a User model and a Property model. I’d like to track who has  
updated a property record, so I added an integer field to the  
properties table called updated\_by. Then I made the following  
changes:

In the User model:  
has\_many :properties

In the Property model:  
belongs\_to :user, :class\_name =\> “User”, :foreign\_key =\>  
‘updated\_by’

Here’s what I see inside of the rails console:

> > prop = Property.first  
> > =\> #\<Property id: 1, name: “Main Street Center”, …, updated\_by: nil\>
> 
> > usr = User.first  
> > =\> #\<User id: 1, login: “sshartles”, name: “Sam Shartles”, … \>
> 
> > prop.user = usr  
> > =\> #\<User id: 1, login: “sshartles”, name: “Sam Shartles”, … \>
> 
> > prop.updated\_by  
> > =\> 1
> 
> > prop.updated\_by.name  
> > NoMethodError: undefined method `name’ for 1:Fixnum  
> > from (irb):5
> 
> >

I’m not sure what I’ve done, but I think it’s something simple. Any  
help would be massively appreciated.

Sam

---

<div class="post-metadata">

**Author:** ![sSshartles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@sSshartles](https://www.ruby-forum.com/u/sSshartles)\
**Post date:** [January 31, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/has-many-with-different-foreign-key/196890/2 "2009-01-31T19:49:00Z")

</div>

s. shartles wrote:

> In the User model:  
> has\_many :properties
> 
> In the Property model:  
> belongs\_to :user, :class\_name =\> “User”, :foreign\_key =\>  
> ‘updated\_by’
> 
> Here’s what I see inside of the rails console:
> 
> > > prop = Property.first  
> > > =\> #\<Property id: 1, name: “Main Street Center”, …, updated\_by: nil\>  
> > > usr = User.first  
> > > =\> #\<User id: 1, login: “sshartles”, name: “Sam Shartles”, … \>  
> > > prop.user = usr  
> > > =\> #\<User id: 1, login: “sshartles”, name: “Sam Shartles”, … \>  
> > > prop.updated\_by  
> > > =\> 1  
> > > prop.updated\_by.name  
> > > NoMethodError: undefined method `name’ for 1:Fixnum  
> > > from (irb):5

Yeah the fixnum error is because updated\_by is just an integer field to  
rails.

If you tried:  
prop.user = usr  
prop.user.name #=\> “Sam Shartles”

This should get you are trying to acheive:

In the User model:  
has\_many :properties, :foreign\_key =\> ‘updated\_by’

In the Property model:  
belongs\_to :updated\_by, :class\_name =\> ‘User’, :foreign\_key =\>  
‘updated\_by’

I must admit in the past I’ve tended to use ‘updated\_by\_id’ instead as  
your field name that way you can access both updated\_by\_id (returns int)  
and updated\_by (returns user).

You may also look at the userstamp plugin:

> **[GitHub - delynn/userstamp: This Rails plugin extends ActiveRecord::Base to add...](https://github.com/delynn/userstamp/tree/master)**
>
> master

Cheers  
Luke
