# Join Tables/Forms

**URL:** <https://www.ruby-forum.com/t/join-tables-forms/114144>\
**Category:** Rails\
**Created:** [September 8, 2007, 3:04pm UTC](https://www.ruby-forum.com/t/join-tables-forms/114144 "2007-09-08T15:04:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [September 8, 2007, 3:04pm UTC](https://www.ruby-forum.com/t/join-tables-forms/114144/1 "2007-09-08T15:04:33Z")

</div>

Hello all. I am hoping someone can clear up a problem I am having.  
Here is the background:  
a) I am using REST (create my scaffolds with scaffold\_resource)  
b) I have installed the RESTful\_Authentication plugin  
c) I have created a model called user (through the RESTFul  
Authentication plugin) and a model called group. The group model is for  
storing information on groups that are centered around a topic.

I now want a database table that will store the users and the groups to  
which they join. So, I created a join table called groups\_users using  
generate model and migrating the data. This is the correct thinking  
right? Then I set up my associations with has\_many in my user and group  
model.

Interesting note is in my mySQL db it creates the db as group\_users and  
not groups\_users. Convention in Rails says to use groups\_users.  
Changing my mySQL db table name to groups\_users will not work and throw  
an error in my rails application. Only way to get it to work was using  
group\_users. Not sure why that happened.

The actual interaction with the application works like this:  
The user creates a group (works fine, no errors in my code, populates  
db). Then they can see the groups (show in my controller, works fine,  
no errors in code). I have a button on my page that says “join group.”  
I have 2 hidden fields that pass the user name (they must be logged in)  
and the group name (value is populated from show action). Clicking on  
the join button displays no errors or problems and redirects to the  
correct view (create.rhtml) but doesn’t populate the db with the  
group\_id, user\_id, group name and username information. Here is my  
code:

group\_users db migration information:  
t.column :group\_id, :integer  
t.column :user\_id, :integer  
t.column :groupname, :string  
t.column :username, :string

my form on the show.rhtml view:  
\<% form\_for :GroupUser, :url=\>{:controller=\>‘groups\_users’,  
:action=\>‘create’} do |f| -%\>  
\<%= f.hidden\_field :groupname, :value=\> @group.name %\>  
\<%= f.hidden\_field :username, :value=\> current\_user.login %\>

\<%= submit\_tag "Join this group" %\>

\<% end -%\>

I had to use :url=\>{:controller=\>‘groups\_users’, :action=\>‘create’  
because I couldn’t get it to work any other way. I don’t think that is  
right for REST.

My GroupsUsers controller:  
def create  
@GroupUser = GroupUser.new(:groupname=\>params[:groupname], :username  
=\> params[:username])  
@GroupUser.save  
end

I have researched this in my Agile Web D. with Rails book and  
understand associations, but it doesn’t talk about using forms and  
associations. Also have peepcode screencasts and Josh S.'s join  
table blog post. While these help with associations, it doesn’t talk  
about my scenario or get me to a point where I can understand… Sorry  
for such a long post, but not sure where to go from here.

thanks.  
Mike R.

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [September 9, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/join-tables-forms/114144/2 "2007-09-09T20:07:59Z")

</div>

Hopefully this will help someone out. I did more research and found  
this:

> **[\#47 Two Many-to-Many - RailsCasts](http://railscasts.com/episodes/47-two-many-to-many)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

It’s a railscasts that focuses on associations specifically  
has\_and\_belongs\_to\_many and has\_many :through. It is a free screencast  
and only a few minutes long. It answered almost all of my questions. I  
recommend it for those who are having trouble with join tables and  
updating a database. I only have to figure out now how to be create a  
form that can update a database using these 2 types of associations.  
I’ll reply with another post once I find the information or figure it  
out.

thanks.  
Mike R.
