# Activerecord relations

**URL:** <https://www.ruby-forum.com/t/activerecord-relations/95512>\
**Category:** Rails\
**Created:** [June 4, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/activerecord-relations/95512 "2006-06-04T20:12:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [June 4, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/activerecord-relations/95512/1 "2006-06-04T20:12:56Z")

</div>

I’ve got two tables set up - Things and Users where Users have many  
things and Things belongs to Users.

They’re connected by Things.user\_id =\> User.id and Things.updated\_by\_id  
=\> User.id both in the Things model using Belongs\_to and custom foreign  
key for one of them.

Problem is when I’ve got the updated\_by\_id belongs\_to I can’t add Things  
using Things.updated\_by\_id = session[:user]  
I get a fixnum error.

But when I remove the foreign key for updated\_by\_id I can’t do  
things.updated\_by\_id.username as username dosn’t exist it says.

Any suggestions?

Thanks!

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [June 4, 2006, 8:20pm UTC](https://www.ruby-forum.com/t/activerecord-relations/95512/2 "2006-06-04T20:20:57Z")

</div>

Adrian wrote:

> I’ve got two tables set up - Things and Users where Users have many  
> things and Things belongs to Users.
> 
> They’re connected by Things.user\_id =\> User.id and Things.updated\_by\_id  
> =\> User.id both in the Things model using Belongs\_to and custom foreign  
> key for one of them.
> 
> Problem is when I’ve got the updated\_by\_id belongs\_to I can’t add Things  
> using Things.updated\_by\_id = session[:user]  
> I get a fixnum error.
> 
> But when I remove the foreign key for updated\_by\_id I can’t do  
> things.updated\_by\_id.username as username dosn’t exist it says.
> 
> Any suggestions?
> 
> Thanks!

Please say exactly what “a fixnum error” is. Fixnum is a class, not an  
error type. Always inlcude the exact error when asking for help.

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [June 4, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/activerecord-relations/95512/3 "2006-06-04T21:03:17Z")

</div>

Sorry about that.  
“User expected, got Fixnum”  
Is the exact error.

Thanks for your reply.

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [June 4, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/activerecord-relations/95512/4 "2006-06-04T21:27:15Z")

</div>

Adrian wrote:

> I’ve got two tables set up - Things and Users where Users have many  
> things and Things belongs to Users.
> 
> They’re connected by Things.user\_id =\> User.id and Things.updated\_by\_id  
> =\> User.id both in the Things model using Belongs\_to and custom foreign  
> key for one of them.
> 
> Problem is when I’ve got the updated\_by\_id belongs\_to I can’t add Things  
> using Things.updated\_by\_id = session[:user]  
> I get a fixnum error.
> 
> But when I remove the foreign key for updated\_by\_id I can’t do  
> things.updated\_by\_id.username as username dosn’t exist it says.
> 
> Any suggestions?

It would help if you would include the associations from your models  
(the has\_many and belongs\_to methods as they appear in the classes). I  
suspect your belongs\_to for updated\_by is faulty.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [June 4, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/activerecord-relations/95512/5 "2006-06-04T21:51:41Z")

</div>

You’re probably right that it’s faulty as belongs\_to :user works and  
belongs\_to :updated\_by\_id dosn’t when they’re basically doing the same  
thing - but I have no idea what it is - maybe I got this whole relations  
thing mixed up where the belongs\_to is looking for updated\_by\_id within  
the User model.

In the thing.rb model  
belongs\_to :user  
belongs\_to :updated\_by\_id,  
:class\_name =\> “User”,  
:foreign\_key =\> “updated\_by\_id”

In the user.rb  
has\_many :things

Thanks again!

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [June 5, 2006, 1:04am UTC](https://www.ruby-forum.com/t/activerecord-relations/95512/6 "2006-06-05T01:04:45Z")

</div>

Adrian wrote:

> You’re probably right that it’s faulty as belongs\_to :user works and  
> belongs\_to :updated\_by\_id dosn’t when they’re basically doing the same  
> thing - but I have no idea what it is - maybe I got this whole relations  
> thing mixed up where the belongs\_to is looking for updated\_by\_id within  
> the User model.

While the whatever\_id field in the DB is important, at the model level  
you should be thinking of the associated thing as an object, not a  
foreign key. Try this:

Thing:  
belongs\_to :user  
belongs\_to :updater, :class\_name =\> “User”, :foreign\_key =\>  
“updater\_id”

User:  
has\_many :things  
has\_many :updated\_things, :class\_name =\> “Thing”, :foreign\_key =\>  
“updater\_id”

Since you have two belongs\_to associations in Thing, you’ll want two  
has\_many associations in User as well. The primary :user/:things pair  
uses the default config options. The update pair needs to specify both  
class name and foreign key, since those can’t be inferred from the  
non-standard association name.

Then, when you are using the association, use it like:  
some\_thing.user = a\_user  
some\_thing.updater = another\_user

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [June 5, 2006, 3:03am UTC](https://www.ruby-forum.com/t/activerecord-relations/95512/7 "2006-06-05T03:03:51Z")

</div>

You rock buddy!

I appreciate your help!
