Forum: Ruby on Rails Activerecord relations.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Adrian (Guest)
on 2006-06-04 22:12
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!
Alex W. (Guest)
on 2006-06-04 22:20
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.
Adrian (Guest)
on 2006-06-04 23:03
Sorry about that.
"User expected, got Fixnum"
Is the exact error.

Thanks for your reply.
Josh S. (Guest)
on 2006-06-04 23:27
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
Adrian (Guest)
on 2006-06-04 23:51
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!
Josh S. (Guest)
on 2006-06-05 03:04
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
Adrian (Guest)
on 2006-06-05 05:03
You rock buddy!

I appreciate your help!
This topic is locked and can not be replied to.