Two belongs_to refering the same model?

Hi,

My task manager has a task model:

class Task < ActiveRecord::Base
belongs_to :project
belongs_to :user # responsible of the task
end

By adding belongs_to :user, my task objets have a user method:
name = Task.find(1).user.name

I’d like to add a creator (or created_by) method pointing to the user
table.
How can I do that ?

Something like belongs_to :user, :name=>‘creator’ # creator of the task,
refering to creator_id in the Task table.

Thanks for your pointers,
Mickael.

Hi,
I would create to foreign keys for tasks, maintainer_id and
creator_id, and then set up the model as follows:
class Task < ActiveRecord::Base
belongs_to :maintainer, :class_name => “User”
belongs_to :creator, :class_name => “User”
end

You can see the documentation for belongs_to here:
http://www.railsdocumentation.org/api/classes/ActiveRecord/Associations/ClassMethods.html#M000761

I hope this helped you.

Greetings
Christoph

On 17 Jul., 23:00, Mickael Faivre-macon <rails-mailing-l…@andreas-

Thanks Christoph! That’s exactly what I needed:
belongs_to creator, :class_name => “user”, :foreign_key => “creator_id”,

Christoph wrote:

class Task < ActiveRecord::Base
belongs_to :maintainer, :class_name => “User”
belongs_to :creator, :class_name => “User”
end

You can see the documentation for belongs_to here:
http://www.railsdocumentation.org/api/classes/ActiveRecord/Associations/ClassMethods.html#M000761

I hope this helped you.

Greetings
Christoph

xiexie Fred,

In the real app code my column is named “created_by”, so if I want a
“creator” I need a :foreign_key => “created_by” (just for info on the
real problem, thanks again for the info)

Mickael.

Frederick C. wrote:

On Jul 18, 7:29�am, Mickael Faivre-macon <rails-mailing-l…@andreas-
s.net> wrote:

Thanks Christoph! That’s exactly what I needed:
belongs_to creator, :class_name => “user”, :foreign_key => “creator_id”,

from rails 2 (possibly earlier; not quite sure) the :foreign key
option isn’t needed in cases like this (it defaults to
association_name_id)

Fred

I’ve been facing a similar problem and the solution of having two
foreign keys sound good but before I implement it, I have a
hypothetical question: suppose I am a user, and I have both created a
few tasks and I’m also the maintainer of a few others. The User
instance is called @ref3at for examples. If I have the following
instruction:
@tasks = @ref3at.tasks So now if I do this: @tasks = @user.tasks
whiich tasks will it retrieve

On Jul 18 2008, 3:50 am, Mickael Faivre-macon <rails-mailing-

On Jul 18, 7:29 am, Mickael Faivre-macon <rails-mailing-l…@andreas-
s.net> wrote:

Thanks Christoph! That’s exactly what I needed:
belongs_to creator, :class_name => “user”, :foreign_key => “creator_id”,

from rails 2 (possibly earlier; not quite sure) the :foreign key
option isn’t needed in cases like this (it defaults to
association_name_id)

Fred

I’ve been facing a very similar problem and have a model that
references another model twice. The two foreign keys solution sounds
good but before implementing it I have a hypothetical question
concerning your situation. Say I am a user and my User instance is
@ref3at. As a user I have created several tasks and I’m also the
maintainer of several others, in this case what happens when the
following instruction is executed:

@tasks = @ref3at.tasks

Does it retrieve the tasks that I have created or the tasks that I’m a
maintainer of?

On Jul 18 2008, 3:50 am, Mickael Faivre-macon <rails-mailing-

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs