Here''s a seemingly simple ActiveRecord use case. I have Projects and Users. Each project has a single team leader as well as a review team that consists of several users. I would like to use the same User model for authentication and just reference these users in my Project model. I tried this but got an "unknown column" error (for User.project_id) class Project has_one :lead, :class_name => ''User'' end Any suggestions on how to model this relationship without requiring references to Project in the User table? From a database table viewpoint I would like create_table :projects do |t| t.integer :lead_id t.timestamps end The review team part seems like it could be done with HBTM. Any thoughts? Sean --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alex Wayne
2008-Apr-07 19:49 UTC
Re: Simple active record issue. I must be missing something.
schof wrote:> Here''s a seemingly simple ActiveRecord use case. I have Projects and > Users. Each project has a single team leader as well as a review team > that consists of several users. I would like to use the same User > model for authentication and just reference these users in my Project > model. > > I tried this but got an "unknown column" error (for User.project_id) > > class Project > has_one :lead, :class_name => ''User'' > end > > Any suggestions on how to model this relationship without requiring > references to Project in the User table? > > From a database table viewpoint I would like > > create_table :projects do |t| > t.integer :lead_id > t.timestamps > end > > The review team part seems like it could be done with HBTM. > > Any thoughts? > > SeanYou want belongs_to. belongs_to :lead, :class_name => ''User'' And, because I forget of the foreign maps to the association name, or the class name, you may need that to be: belongs_to :lead, :class_name => ''User'', :foreign_key => ''lead_id'' -- 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 -~----------~----~----~----~------~----~------~--~---
Of course belongs_to! Thanks. (I told you it was something obvious.) I think I was getting hung up on :class_name which I have not used before. Sean On Apr 7, 3:49 pm, Alex Wayne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> schof wrote: > > Here''s a seemingly simple ActiveRecord use case. I have Projects and > > Users. Each project has a single team leader as well as a review team > > that consists of several users. I would like to use the same User > > model for authentication and just reference these users in my Project > > model. > > > I tried this but got an "unknown column" error (for User.project_id) > > > class Project > > has_one :lead, :class_name => ''User'' > > end > > > Any suggestions on how to model this relationship without requiring > > references to Project in the User table? > > > From a database table viewpoint I would like > > > create_table :projects do |t| > > t.integer :lead_id > > t.timestamps > > end > > > The review team part seems like it could be done with HBTM. > > > Any thoughts? > > > Sean > > You want belongs_to. > > belongs_to :lead, :class_name => ''User'' > > And, because I forget of the foreign maps to the association name, or > the class name, you may need that to be: > > belongs_to :lead, :class_name => ''User'', :foreign_key => ''lead_id'' > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---