Keith Bottner
2006-May-11 19:56 UTC
[Rails] ActiveRecord collection_select and has_and_belongs_to_many
Hello everyone, first post to the list and a relative newbie to Rails development. Done quite a bit of JSP, PHP, HTML, XML etc and thought I would kick the tires on Rails to see if it can speed up development for internal applications. Anyway, I am running into a problem that is just driving me crazy and everything I read on the net doesn''t seem to help. I was hoping someone here might have had the same problem and be able to point me in the right direction. Database Schema: table employees id name table projects id name table project_roles id name table project_members id employee_id project_id role_id I then have the following ActiveRecord objects: class Employee < ActiveRecord::Base has_and_belongs_to_many :project_members, :join_table => "project_members", :class_name => "ProjectMember" end class Project < ActiveRecord::Base has_and_belongs_to_many :project_members, :join_table => "project_members", :class_name => "ProjectMember" end class ProjectRole < ActiveRecord::Base has_and_belongs_to_many :project_members, :join_table => "project_members", :class_name => "ProjectMember" end class ProjectMember < ActiveRecord::Base belongs_to :project belongs_to :employee belongs_to :project_role set_table_name "project_members" end The next thing I want to do is to display a multi-select list box, on the project form, and display all of the employees that when selected will add the proper entries into the project_members table. I would be happy to write a method that gets called on submission and transforms the @params into the appropriate ActiveRecord requests, but I cannot get the list to display with collection_select no matter what I try. Can someone point me in the right direction? Am I using the has_and_belongs_to_many relationship wrong? Is it useless to try to get this kind of a relationship to show up using collection_select? Is there some other parameters that I need to use that I am not? Is there a better pattern for these type of relationships with regards to integrating them into the MVC paradigm? Any help would be appreciated as I am Googled out! Keith
Stephen Bartholomew
2006-May-11 21:36 UTC
[Rails] ActiveRecord collection_select and has_and_belongs_to_many
You might wan to have a look at through associations for this: http://wiki.rubyonrails.com/rails/pages/ThroughAssociations. I''ve also found that using checkboxes is easier for habtm relationships. Take the following relationship: class Project < ActiveRecord::Base has_and_belongs_to_many :members end class Member < ActiveRecord::Base has_and_belongs_to_many :projects end Your ''edit member'' view might show a list of checkboxes for each project: <% for project in @projects %> <%=check_box_tag name="member[project_ids]", project.id, @member.projects.include?(project) %> <% end %> When this is submitted rails will automatically update the joiner table with the checked associations. Also, I might be wrong in your case, but you shouldn''t have to specify all the table data in your models - Rails will pick all that up for you. For example: Take the following tables: parents children You models will look like this: class Parent < ActiveRecord::Base has_many :children end class Child < ActiveRecord::Base belongs_to :parent end Rails will do all the hard work for you. Wow - i really got going on that reply :0) Hope it helps, Steve Keith Bottner wrote:> Hello everyone, first post to the list and a relative newbie to Rails > development. Done quite a bit of JSP, PHP, HTML, XML etc and thought I would > kick the tires on Rails to see if it can speed up development for internal > applications. Anyway, I am running into a problem that is just driving me > crazy and everything I read on the net doesn''t seem to help. I was hoping > someone here might have had the same problem and be able to point me in the > right direction. > > Database Schema: > > table employees > id > name > > table projects > id > name > > table project_roles > id > name > > table project_members > id > employee_id > project_id > role_id > > I then have the following ActiveRecord objects: > > class Employee < ActiveRecord::Base > has_and_belongs_to_many :project_members, :join_table => > "project_members", :class_name => "ProjectMember" > end > > class Project < ActiveRecord::Base > has_and_belongs_to_many :project_members, :join_table => > "project_members", :class_name => "ProjectMember" > end > > class ProjectRole < ActiveRecord::Base > has_and_belongs_to_many :project_members, :join_table => > "project_members", :class_name => "ProjectMember" > end > > class ProjectMember < ActiveRecord::Base > belongs_to :project > belongs_to :employee > belongs_to :project_role > set_table_name "project_members" > end > > The next thing I want to do is to display a multi-select list box, on the > project form, and display all of the employees that when selected will add > the proper entries into the project_members table. I would be happy to write > a method that gets called on submission and transforms the @params into the > appropriate ActiveRecord requests, but I cannot get the list to display with > collection_select no matter what I try. > > Can someone point me in the right direction? Am I using the > has_and_belongs_to_many relationship wrong? Is it useless to try to get this > kind of a relationship to show up using collection_select? Is there some > other parameters that I need to use that I am not? Is there a better pattern > for these type of relationships with regards to integrating them into the > MVC paradigm? > > Any help would be appreciated as I am Googled out! > > Keith > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >