Join Tables/Forms

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.

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

http://railscasts.com/episodes/47

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs