Hi, I cannot seem to create associations like this: class Project < ActiveRecord::Base belongs_to :manager, :class_name => ''User'', :foreign_key => ''manager'' belongs_to :liaison, :class_name => ''User'', :foreign_key => ''liaison'' end p = Project.new p.manager => 1 trying to retrieve associated objects doesn''t work and I only receive the associated record''s ID value as a Fixnum instance. Is there any way to implement this particular association? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060214/888363c2/attachment.html
I guess you should name you association different to the table''s field name. Kent. On 2/14/06, Douglas Tan <bianster@gmail.com> wrote:> Hi, > > I cannot seem to create associations like this: > > class Project < ActiveRecord::Base > belongs_to :manager, :class_name => ''User'', :foreign_key => ''manager'' > belongs_to :liaison, :class_name => ''User'', :foreign_key => ''liaison'' > end > > p = Project.new > p.manager > => 1 > > trying to retrieve associated objects doesn''t work and I only receive the > associated record''s ID value as a Fixnum instance. Is there any way to > implement this particular association? > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
On 2/14/06, Kent Sibilev <ksruby@gmail.com> wrote:> I guess you should name you association different to the table''s field name.Exactly right, that''s how I got this sort of setup running. You should try belongs_to :manager_user, :class_name => ''User'', :foreign_key => ''manager'' belongs_to :liaison_user, :class_name => ''User'', :foreign_key => ''liaison'' or somesuch. Then call project.manager_user when you want the user. Either that or rename your "manager" column to "manager_id" in the db. In my class it''s like: class Bug < ActiveRecord::Base <snip> belongs_to :opened_by_user, :class_name => "User", :foreign_key => "opened_by" belongs_to :assigned_to_user, :class_name => "User", :foreign_key => "assigned_to" end class Bug < ActiveRecord::Base <snip> belongs_to :opened_by_user, :class_name => "User", :foreign_key => "opened_by" belongs_to :assigned_to_user, :class_name => "User", :foreign_key => "assigned_to" end
sorry I mispasted my User class: class User < ActiveRecord::Base <snip> has_many :bugs_opened, :class_name => "Bug", :foreign_key => "opened_by" has_many :bugs_assigned, :class_name => "Bug", :foreign_key => "assigned_to"
hmm, I got it to work by appending ''_id'' to the :foreign_key values, like so: ''manager_id'' On 2/15/06, Ryan Tate <lists@ryantate.com> wrote:> > sorry I mispasted my User class: > > class User < ActiveRecord::Base > <snip> > has_many :bugs_opened, :class_name => "Bug", :foreign_key => "opened_by" > > has_many :bugs_assigned, :class_name => "Bug", :foreign_key => > "assigned_to" > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060215/e5af1225/attachment.html
Douglas Tan wrote:> hmm, I got it to work by appending ''_id'' to the :foreign_key values, > like > so: ''manager_id''I imagine you don''t then need :foreign_key => ''manager_id'' I bet RoR will make that association for you _T -- Posted via http://www.ruby-forum.com/.
> I imagine you don''t then need :foreign_key => ''manager_id'' I bet RoR > will make that association for you > > _TI wish; it took my hours to find you NEED to define foreign_key if you use class_name -- 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 -~----------~----~----~----~------~----~------~--~---