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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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- s.net> wrote:> 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. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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> 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/Associatio... > > > I hope this helped you. > > > Greetings > > Christoph > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---