Hello, I''m an almost-not-a-beginner-anymore, and I''ve just run into a tricky problem. Say I have a class User, a class Project, and a many-to-many relationship between the two. In the users_projects table, there is a field is_manager to specify the relationship to a given project. What I want to get is the Project class having one :manager, as in :has_one :manager. The issue is that :conditions for :has_one does seem to only take the User''s table in consideration. So is there some way to define a :manager (and if possible a set of :submanagers with an :has_many clause) with the relationship table? In the worst case, I''ll just have a is_manager?(project_id) function for the User model, but if there is a way to define Project so that it has a special kinds of users...) Thanks in advance, -- Michel Valdrighi Devéloppeur Web Intraordinaire http://zengun.org/weblog/
I have no idea if this would work, as I haven''t tried it, but maybe... has_one :manager, :class_name=>"User", :conditions=>" id in (select user_id from projects_users where project_id=#{id})" On 9/15/05, Michel Valdrighi <michelv-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello, > > I''m an almost-not-a-beginner-anymore, and I''ve just run into a tricky > problem. > > Say I have a class User, a class Project, and a many-to-many > relationship between the two. > In the users_projects table, there is a field is_manager to specify > the relationship to a given project. > > What I want to get is the Project class having one :manager, as in > :has_one :manager. > The issue is that :conditions for :has_one does seem to only take the > User''s table in consideration. > > So is there some way to define a :manager (and if possible a set of > :submanagers with an :has_many clause) with the relationship table? > > In the worst case, I''ll just have a is_manager?(project_id) function > for the User model, but if there is a way to define Project so that it > has a special kinds of users...) > > Thanks in advance, > > -- > Michel Valdrighi > Devéloppeur Web Intraordinaire > http://zengun.org/weblog/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I forgot to add something has_one :manager, :class_name=>"User", :conditions=>" id in (select user_id from projects_users where project_id=#{id} and is_manager=1)" On 9/15/05, Matt Pantana <matt.pantana-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I have no idea if this would work, as I haven''t tried it, but maybe... > has_one :manager, > :class_name=>"User", > :conditions=>" id in (select user_id from projects_users where > project_id=#{id})" > > > On 9/15/05, Michel Valdrighi <michelv-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello, > > > > I''m an almost-not-a-beginner-anymore, and I''ve just run into a tricky > > problem. > > > > Say I have a class User, a class Project, and a many-to-many > > relationship between the two. > > In the users_projects table, there is a field is_manager to specify > > the relationship to a given project. > > > > What I want to get is the Project class having one :manager, as in > > :has_one :manager. > > The issue is that :conditions for :has_one does seem to only take the > > User''s table in consideration. > > > > So is there some way to define a :manager (and if possible a set of > > :submanagers with an :has_many clause) with the relationship table? > > > > In the worst case, I''ll just have a is_manager?(project_id) function > > for the User model, but if there is a way to define Project so that it > > has a special kinds of users...) > > > > Thanks in advance, > > > > -- > > Michel Valdrighi > > Devéloppeur Web Intraordinaire > > http://zengun.org/weblog/ > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 15.9.2005, at 21.58, Matt Pantana wrote:> I forgot to add something > > has_one :manager, > :class_name=>"User", > :conditions=>" id in (select user_id from projects_users where > project_id=#{id} and is_manager=1)" >This is not how has_one (or other association methods for that matter) works. Please read the api docs [1] for an explanation. Amy Hoy''s cheat sheet [2] might also come in handy. //jarkko [1] http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ ClassMethods.html [2] http://www.slash7.com/cheats/activerecord_cheatsheet.pdf> > > On 9/15/05, Matt Pantana <matt.pantana-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > I have no idea if this would work, as I haven''t tried it, but maybe... > > has_one :manager, > :class_name=>"User", > :conditions=>" id in (select user_id from projects_users where > project_id=#{id})" > > > > On 9/15/05, Michel Valdrighi <michelv-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > wrote: Hello, > > I''m an almost-not-a-beginner-anymore, and I''ve just run into a > tricky problem. > > Say I have a class User, a class Project, and a many-to-many > relationship between the two. > In the users_projects table, there is a field is_manager to specify > the relationship to a given project. > > What I want to get is the Project class having one :manager, as in > :has_one :manager. > The issue is that :conditions for :has_one does seem to only take the > User''s table in consideration. > > So is there some way to define a :manager (and if possible a set of > :submanagers with an :has_many clause) with the relationship table? > > In the worst case, I''ll just have a is_manager?(project_id) function > for the User model, but if there is a way to define Project so that it > has a special kinds of users...) > > Thanks in advance, > > -- > Michel Valdrighi > Devéloppeur Web Intraordinaire > http://zengun.org/weblog/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi Michel, On 15.9.2005, at 21.08, Michel Valdrighi wrote:> Hello, > > I''m an almost-not-a-beginner-anymore, and I''ve just run into a > tricky problem. > > Say I have a class User, a class Project, and a many-to-many > relationship between the two. > In the users_projects table, there is a field is_manager to specify > the relationship to a given project. > > What I want to get is the Project class having one :manager, as in > :has_one :manager. > The issue is that :conditions for :has_one does seem to only take the > User''s table in consideration.Forget has_one. This is not a case where it is applicable.> > So is there some way to define a :manager (and if possible a set of > :submanagers with an :has_many clause) with the relationship table?Many ways :-) If you only needed one manager per project, you could add another association between users and projects: class User < ... has_and_belongs_to_many :projects # the existing association has_many :managed_projects, :class_name => "Project", :foreign_key => "manager_id" end class Project < ... has_and_belongs_to_many :users # the existing association belongs_to :manager, :class_name => "User", :foreign_key => "manager_id" end Then you''d also need to add a field manager_id to your projects table. However, this doesn''t work with multiple submanagers. Fortunately, you can add a "type" field to your join table (projects_users). Then you can have as many types of managers per project as you want to. If you want to restrict a project to have a maximum of one manager, you need to put in some callbacks that check for existing managers. class Project ... ... def manager users.find_by_type("manager") rescue nil end def manager=(user) return false if manager # there is already a manager in this project users.push_with_attributes(user, "type" => "manager") end def submanagers users.find_all_by_type("submanager") end ... end ActiveRecord api docs [1] give you more information, this was just a quick example of how you could solve your problem. //jarkko [1] http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ ClassMethods.html> > In the worst case, I''ll just have a is_manager?(project_id) function > for the User model, but if there is a way to define Project so that it > has a special kinds of users...) > > Thanks in advance, > > -- > Michel Valdrighi > Devéloppeur Web Intraordinaire > http://zengun.org/weblog/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hello again, I found a solution! :) (But it''s not pretty. Skip to the bottom for the solution.) On 9/15/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> > On 15.9.2005, at 21.58, Matt Pantana wrote: > > has_one :manager, > > :class_name=>"User", > > :conditions=>" id in (select user_id from projects_users where > > project_id=#{id} and is_manager=1)"Thanks for the idea, unfortunately I''m stuck on MySQL 4.1 (partly by habit, partly by fear of change, mainly by pragmatism), that does not support subselects.> This is not how has_one (or other association methods for that > matter) works. Please read the api docs [1] for an explanation. Amy > Hoy''s cheat sheet [2] might also come in handy.Thanks for the reference to the cheat sheet! I was getting a bit discouraged to make my own cheatsheet after skimming through the Agile book''s appendice on AR... Anyway, here''s a solution I found, it''s not very pretty as it''s adhoc, but it works: has_many :managers, :class_name => "User", :finder_sql => ''SELECT DISTINCT u.* '' + ''FROM users u, projects_users pu '' + ''WHERE pu.project_id = #{id} AND pu.user_id = u.id AND pu.is_manager = 1 '' + ''ORDER BY u.id ASC'' The bad thing is that it uses direct SQL, so if there is an alternate solution I''m all ears (err, eyes). Ciao, -- Michel Valdrighi Devéloppeur Web Intraordinaire http://zengun.org/weblog/