Activerecord relations

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!

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.

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

Thanks for your reply.

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

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!

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

You rock buddy!

I appreciate your help!