# Problem with saving id's in a join table

**URL:** <https://www.ruby-forum.com/t/problem-with-saving-ids-in-a-join-table/53861>\
**Category:** Rails\
**Created:** [February 15, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/problem-with-saving-ids-in-a-join-table/53861 "2006-02-15T14:56:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nside](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/96bed5/32.png) [@nside](https://www.ruby-forum.com/u/nside)\
**Post date:** [February 15, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/problem-with-saving-ids-in-a-join-table/53861/1 "2006-02-15T14:56:03Z")

</div>

hello,

I’am new to ROR and I have a problem.  
I have to models “rights” and “roles” on a has\_and\_belongs\_to\_many  
relationship, through “roles\_rights” table. When I try to add a new role  
although I put in my form fields for each right, a record is added to  
table roles but none to roles\_rights.

I am using Ruby 1.8.4 rails 1.0 mysql 5.0

# here is my code:

# controller:

# def create @role = Role.new(params[:role]) @role.rights = Right.find(@params[:right\_ids])if @params[:right\_ids] if @role.save flash[:notice] = ‘Role was successfully created.’ redirect\_to :action =\> ‘list’ else render :action =\> ‘new’ end end

# view:

Name  
\<%= text\_field 'role', 'name' %\>

Rights  

 \<% for right in @rights %\> - \<%= right.name %\>

```
<% end %>
</ul>

```

# ================================== model:

class Role \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
has\_and\_belongs\_to\_many :rights, :join\_table =\> ‘roles\_rights’  
end

class Right \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :roles, :join\_table =\> ‘roles\_rights’  
end

---

<div class="post-metadata">

**Author:** ![nside](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/96bed5/32.png) [@nside](https://www.ruby-forum.com/u/nside)\
**Post date:** [February 15, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/problem-with-saving-ids-in-a-join-table/53861/2 "2006-02-15T15:32:16Z")

</div>

the way collections work is that 1) they are saved immediately if the  
parent  
record is not a new record or 2) are saved when the parent record is  
saved.  
your situation clearly falls under #2. so they should be save when you  
save  
the @role record. since this isn’t happening, it’s possible that there  
are  
no rights in the collection.

i might suggest adding a breakpoint after

@role.rights = Right.find(@params[:right\_ids])if @params[:right\_ids]

run script/breakpointer in a shell

then load the app and create a new role. in the breakpointer, you can  
then  
examine the vars to make sure they have the values you expect them to  
have.  
CTRL-D to end the breakpoint session.

@params[:right\_ids]  
@role.rights

---

<div class="post-metadata">

**Author:** ![nside](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/96bed5/32.png) [@nside](https://www.ruby-forum.com/u/nside)\
**Post date:** [February 15, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/problem-with-saving-ids-in-a-join-table/53861/3 "2006-02-15T16:25:29Z")

</div>

Bogdan Nenu wrote:

> hello,
> 
> I’am new to ROR and I have a problem.  
> I have to models “rights” and “roles” on a has\_and\_belongs\_to\_many  
> relationship, through “roles\_rights” table. When I try to add a new role  
> although I put in my form fields for each right, a record is added to  
> table roles but none to roles\_rights.

10 to 1 this is the model with HABTO \< 1 bug.

Theres a fix you have to apply to active record

A quick google will find it for you.

---

<div class="post-metadata">

**Author:** ![nside](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/96bed5/32.png) [@nside](https://www.ruby-forum.com/u/nside)\
**Post date:** [February 16, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/problem-with-saving-ids-in-a-join-table/53861/4 "2006-02-16T13:38:51Z")

</div>

On Feb 15, 2006, at 16:25, AC Green wrote:

> 10 to 1 this is the model with HABTO \< 1 bug.
> 
> Theres a fix you have to apply to active record
> 
> A quick google will find it for you.

I need to be aware of this gotcha. Would you please provide some  
keywords to search for? “HABTO active record” returned nothing relevant.

– fxn

---

<div class="post-metadata">

**Author:** ![nside](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/96bed5/32.png) [@nside](https://www.ruby-forum.com/u/nside)\
**Post date:** [February 16, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/problem-with-saving-ids-in-a-join-table/53861/5 "2006-02-16T14:17:25Z")

</div>

Xavier N. wrote:

> On Feb 15, 2006, at 16:25, AC Green wrote:
> 
> > 10 to 1 this is the model with HABTO \< 1 bug.
> > 
> > Theres a fix you have to apply to active record
> > 
> > A quick google will find it for you.
> 
> I need to be aware of this gotcha. Would you please provide some  
> keywords to search for? “HABTO active record” returned nothing relevant.
> 
> – fxn

@params[:rights\_ids] came OK, but the @role.rights does not seem to be  
ok for some reason. It might be some problem with my line:

@role.rights = Right.find(@params[:right\_ids]) if @params[:right\_ids]
