# Inserting data into a JOIN table!

**URL:** <https://www.ruby-forum.com/t/inserting-data-into-a-join-table/148688>\
**Category:** Rails\
**Created:** [September 16, 2008, 8:46am UTC](https://www.ruby-forum.com/t/inserting-data-into-a-join-table/148688 "2008-09-16T08:46:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [September 16, 2008, 8:46am UTC](https://www.ruby-forum.com/t/inserting-data-into-a-join-table/148688/1 "2008-09-16T08:46:38Z")

</div>

Hello,

I have a roles table, permissions table and a rights join table. I  
have has\_many :through relationships between the two tables.

Roles Permissions Rights table  
id name id name role\_id permission\_id  
1 User 1 posts/new 1 2  
2 Admin 2 posts/create 2 2

In the admin side of the app I create new roles and permissions. But I  
wanted to be able to assign values to the rights table in an easy way,  
so I created a rights controller and a view to show the current values  
and assign new permission to certain roles via checkboxes.

Permissions User Admin  
posts/new   
posts/edit   
posts/destroy   
posts/create

I have some code on my view where I save the permission\_ids selected  
onto a hash, but I’ll like to save the roles\_ids too. How can I post  
both of them to save them later on the controller in the rights table?  
The problem that I have is that I can post a hash of permission\_ids,  
but I also need the role\_ids hash. The examples I have looked at only  
need to post one hash and the other value needed to fill the join  
table is the current\_user id. In this case I need permission and role  
ids. Any ideas on how to post this values into the controller?

Portion of my view

```
  <% for permission in Permission.find(:all, :order => "name ASC")

```

%\>  
\<tr class=\<%= cycle(‘even’,‘odd’) %\>\>

\<%=h permission.name %\>

```
      <% for role in Role.find(:all) %>
        <td><%= check_box_tag "right[permission_ids][]",

```

permission.id, permission.roles.include?(role) %\>  
\<% end %\>  
  
\<% end %\>

Thanks!

Elioncho
