Forum: Ruby on Rails many to many relationship and update problem

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.
074c7a541f54e72888d399089d940e2e?d=identicon&s=25 Nam Kim (namwkim)
on 2009-05-09 18:07
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------------------------------------------
<td align="right">Roles:</td>
  <td><%= updating ? collection_select(:role, :id, @roles, "id",
"name"):
    (@user.visible_roles.collect { |role| role.name }.sort.join(", "))%>
</td></tr>


-------------------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])
074c7a541f54e72888d399089d940e2e?d=identicon&s=25 Nam Kim (namwkim)
on 2009-05-09 21:24
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------------------------------------------
> <td align="right">Roles:</td>
>   <td><%= updating ? collection_select(:role, :id, @roles, "id",
> "name"):
>     (@user.visible_roles.collect { |role| role.name }.sort.join(", "))%>
> </td></tr>
>
>
> -------------------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])
This topic is locked and can not be replied to.