Forum: Ruby on Rails problem with saving id's in a join table

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5a98a322a07967c16868187cf946217a?d=identicon&s=25 Bogdan Nenu (nside)
on 2006-02-15 14:56
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:
============================
<p><label for="role_name">Name</label><br/>
<%= text_field 'role', 'name'  %></p>

<div><label for="role_right">Rights</label><br/>
<ul>
	<% for right in @rights %>
		<li><input type="checkbox".
				id ="<%= right.id %>".
				name="right_ids[]".
				value="<%= right.id %>".
		> <%= right.name %></li>

	<% 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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-02-15 15:32
(Received via mailing list)
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
F59329dc91cba06600ff65c85fd3e93c?d=identicon&s=25 AC Green (Guest)
on 2006-02-15 16:25
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.
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2006-02-16 13:38
(Received via mailing list)
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
5a98a322a07967c16868187cf946217a?d=identicon&s=25 Bogdan Nenu (nside)
on 2006-02-16 14:17
Xavier Noria 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]
This topic is locked and can not be replied to.