# ActiveRecord collection\_select and has\_and\_belongs\_to\_many

**URL:** <https://www.ruby-forum.com/t/activerecord-collection-select-and-has-and-belongs-to-many/60291>\
**Category:** Rails\
**Created:** [May 11, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/activerecord-collection-select-and-has-and-belongs-to-many/60291 "2006-05-11T21:57:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Keith\_Bottner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@Keith\_Bottner](https://www.ruby-forum.com/u/Keith_Bottner)\
**Post date:** [May 11, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/activerecord-collection-select-and-has-and-belongs-to-many/60291/1 "2006-05-11T21:57:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Keith\_Bottner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@Keith\_Bottner](https://www.ruby-forum.com/u/Keith_Bottner)\
**Post date:** [May 11, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/activerecord-collection-select-and-has-and-belongs-to-many/60291/2 "2006-05-11T23:37:37Z")

</div>

You might wan to have a look at through associations for this:  
[http://wiki.rubyonrails.com/rails/pages/ThroughAssociations](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
