# Not work has\_many :through association

**URL:** <https://www.ruby-forum.com/t/not-work-has-many-through-association/189135>\
**Category:** Rails\
**Created:** [May 25, 2010, 8:32am UTC](https://www.ruby-forum.com/t/not-work-has-many-through-association/189135 "2010-05-25T08:32:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [May 25, 2010, 8:32am UTC](https://www.ruby-forum.com/t/not-work-has-many-through-association/189135/1 "2010-05-25T08:32:17Z")

</div>

Not work has\_many :through association in rails 2.3.5

Models:

class Role \< ActiveRecord::Base  
attr\_accessible :name

has\_many :assignments  
has\_many :users, :through =\> :assignments  
end

class User \< ActiveRecord::Base  
attr\_accessible :name

has\_many :assignments  
has\_many :roles, :through =\> :assignments  
end

class Assignment \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :role  
end

In the view form of user

```
- for user in User.all
  = check_box_tag 'role[user_ids][]', user.id, @role.users.include?

```

(user)  
=h user.name

When I create a new user, nothing happens, the array of user\_ids is  
empty

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [May 25, 2010, 1:37pm UTC](https://www.ruby-forum.com/t/not-work-has-many-through-association/189135/2 "2010-05-25T13:37:16Z")

</div>

I have a typical method in controller:

def create  
@user = User.new(params[:user])  
if @user.save  
flash[:notice] = “Successfully created user.”  
redirect\_to @user  
else  
render :action =\> ‘new’  
end  
end

I thought it automatically saves the attribute :role\_ids

I need to explicitly save it?

On 25 mayo, 10:23, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [May 25, 2010, 10:25am UTC](https://www.ruby-forum.com/t/not-work-has-many-through-association/189135/3 "2010-05-25T10:25:16Z")

</div>

On May 24, 11:08 pm, Alexander [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In the view form of user
> 
> ```
> - for user in User.all
> = check_box_tag 'role[user_ids][]', user.id, @role.users.include?
> 
> ```
> 
> (user)  
> =h user.name
> 
> When I create a new user, nothing happens, the array of user\_ids is  
> empty

Those checkboxes will result in params[‘role[user\_ids]’] being set to  
an array of user ids - What does your controller code look like? If  
it’s just a classic User.create params[:user] that clearly isn’t being  
used at all

Fred

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [May 25, 2010, 1:58pm UTC](https://www.ruby-forum.com/t/not-work-has-many-through-association/189135/4 "2010-05-25T13:58:21Z")

</div>

On May 25, 12:35 pm, Alexander [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> I thought it automatically saves the attribute :role\_ids

But that’s not what your form (at least not in your initial post) is  
doing - it’s setting the attribute :user\_ids in params[:role] which  
you aren’t using. Did you actually mean to create a checkbox  
user[role\_ids][] for each role ?

Fred

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [May 25, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/not-work-has-many-through-association/189135/5 "2010-05-25T17:05:07Z")

</div>

On May 25, 2:45 pm, Alexander [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> you are right, my mistake  
> in the user form i have this
> 
> ```
> - for role in Role.all
> = check_box_tag "user[role_ids][]", role.id, @user.roles.include?
> 
> ```
> 
> (role)  
> =h role.name

If you look at your development log file does it actually look like  
those parameters are being submitted as you expected ?

Fred

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [May 25, 2010, 3:47pm UTC](https://www.ruby-forum.com/t/not-work-has-many-through-association/189135/6 "2010-05-25T15:47:15Z")

</div>

you are right, my mistake  
in the user form i have this

```
- for role in Role.all
  = check_box_tag "user[role_ids][]", role.id, @user.roles.include?

```

(role)  
=h role.name

On 25 mayo, 13:56, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [May 25, 2010, 5:08pm UTC](https://www.ruby-forum.com/t/not-work-has-many-through-association/189135/7 "2010-05-25T17:08:03Z")

</div>

i found the solution, it was so simple as add this to model of user  
attr\_accessible :role\_ids

anyway thanks for all your answers
