Associations question

Hey everyone,

I have a quick question on a tricky model for the app I’m working on.
I have a Tasks model, and a Task can be assigned to a User; a Task
should also know the User who assigned (created) it, and be updatable
by that user. The idea is so the View can have something like on a
“My Tasks” page:

Assigned by John S. on June 1st, 2008

Since it has to have two references to the same model (User), one for
who has been assigned to, and one for who did the assigning, what
association should I be looking at? The associations in Rails are a
little confusing beyond the basic belongs_to/has_one/has_many things,
so I’m not sure what I should use. has_and_belongs_to_many?
has_many :through?

Any help would be appreciated.

  • Wayne

fields in tasks: user_id and assigning_user_id

in the task model:

belongs_to :user
belongs_to :assigning_user, :class => “User”

in the user model:

has_many :tasks
has_many :assigned_tasks, :as => “assigning_user”

On Thu, May 29, 2008 at 11:42 AM, Wayne M. [email protected]
wrote:


Appreciated my help?
Recommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

On 29 May 2008, at 03:15, Ryan B. (Radar) wrote:

has_many :assigned_tasks, :as => “assigning_user”

that should be :foreign_key => “assigning_user_id”.
I wrote up a reasonably detailed walkthrough of this sort of thing at

Fred

I’ll check that link out - thanks all!

  • Wayne