# Many to many relationship and update problem

**URL:** <https://www.ruby-forum.com/t/many-to-many-relationship-and-update-problem/167588>\
**Category:** Rails\
**Created:** [May 9, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-and-update-problem/167588 "2009-05-09T18:07:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![namwkim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b3f665/32.png) [@namwkim](https://www.ruby-forum.com/u/namwkim)\
**Post date:** [May 9, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-and-update-problem/167588/1 "2009-05-09T18:07:34Z")

</div>

I am new to Ruby on Rails. Actually I just started two days ago… I am  
working o existing code and having a problem. we have two database  
tables called ‘users’ and ‘roles’ they are many-to-many relationship and  
linked through mapping table called roles\_users. I want to update user’s  
role, but I have no idea how to do, primarily because roles are not  
inherent members of users. The following is the my code snippet, please  
help me out. Please also let me know if you need clarification. Thanks.

Nam

- Currently, I am assuming that users have two roles, but they can have  
multiple roles. So don’t be confused.

-------------------In  
\_form.rhtml------------------------------------------

Roles: \<%= updating ? collection\_select(:role, :id, @roles, "id", "name"): (@user.visible\_roles.collect { |role| role.name }.sort.join(", "))%\>

-------------------In update action in  
â€˜user\_controller.rbâ€™----------------  
@user = User.find(params[:id])  
@new\_role = Role.find(params[:role][:id])  
@old\_role = @user.visible\_roles.collect { |role| role.name  
}.sort.join(", ")  
@old\_role = Role.find\_by\_name(@old\_role)  
@user.roles.each\_index{|index|  
if (@user.roles[index].id == @old\_role.id)  
@user.roles[index] = @new\_role  
end  
}  
prior\_approval = @user.approved  
#@user.update\_attributes({:roles=\>@user.roles})  
@user.update\_attributes(params[:user])

---

<div class="post-metadata">

**Author:** ![namwkim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b3f665/32.png) [@namwkim](https://www.ruby-forum.com/u/namwkim)\
**Post date:** [May 9, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-and-update-problem/167588/2 "2009-05-09T21:24:54Z")

</div>

I solved the problem. Yeah I am stupid  
Here is the code snippet which solved the problem I had.

```
  @user.roles.delete(@old_role)

  @user.roles<<@new_role

```

Nam Kim wrote:

> I am new to Ruby on Rails. Actually I just started two days ago… I am  
> working o existing code and having a problem. we have two database  
> tables called ‘users’ and ‘roles’ they are many-to-many relationship and  
> linked through mapping table called roles\_users. I want to update user’s  
> role, but I have no idea how to do, primarily because roles are not  
> inherent members of users. The following is the my code snippet, please  
> help me out. Please also let me know if you need clarification. Thanks.
> 
> Nam
> 
> - Currently, I am assuming that users have two roles, but they can have  
> multiple roles. So don’t be confused.
> 
> -------------------In  
> \_form.rhtml------------------------------------------
> 
> Roles: \<%= updating ? collection\_select(:role, :id, @roles, "id", "name"): (@user.visible\_roles.collect { |role| role.name }.sort.join(", "))%\>
> 
> -------------------In update action in  
> â€˜user\_controller.rbâ€™----------------  
> @user = User.find(params[:id])  
> @new\_role = Role.find(params[:role][:id])  
> @old\_role = @user.visible\_roles.collect { |role| role.name  
> }.sort.join(", ")  
> @old\_role = Role.find\_by\_name(@old\_role)  
> @user.roles.each\_index{|index|  
> if (@user.roles[index].id == @old\_role.id)  
> @user.roles[index] = @new\_role  
> end  
> }  
> prior\_approval = @user.approved  
> #@user.update\_attributes({:roles=\>@user.roles})  
> @user.update\_attributes(params[:user])
