如何在多对多关系中存储大量数据?铁轨5

时间:2018-01-20 17:53:47

标签: ruby-on-rails devise

好人,我在rails 5中执行管理系统,而且我正在通过许可部分,基本上我希望用户有很多组,而且很多组都有很多用户,我知道它通过关系有很多,我知道如何从控制台配对,但我只知道如何占用用户和组,例如:

user = User.create name: "Luis", email: "example@email.com"
group = Group.create name: "Development", description: "Those that get screwed thanks to the design >:v"
group2 = Group.create name: "Design", description: "The creatives"
grupo3 = Group.create name: "Ventas", description: "The annoying bugs that only think about making money overnight"
membership = Membership.create group: group, user: user

控制器是没有任何添加的基本支架,用户通过设计注册

在创建用户管理其用户组时,我希望管理员在创建用户组时管理用户,反之亦然。

我不希望管理员一次必须标记一个组,但是从视图中选择几个组(开发,设计,招聘),并且rails会立即将它们全部保存。也不是您使用可能存在的不同组多次注册同一用户。

1 个答案:

答案 0 :(得分:0)

这可能是大量的语法错误,因为我没有测试过,但你可以理解并纠正它。

用户模型

class User < ApplicationRecord
  has_many :memberships
  has_many :groups, through: :memberships
end

群组模型

class Group < ApplicationRecord
  has_many :memberships
  has_many :users, through: :memberships
end

会员资格模式

class Membership < ApplicationRecord
  belongs_to :group
  belongs_to :user
end

UsersController

def edit
  @user = User.find_by(id: params[:id]) 
  @allGroups = Group.all                
  @groupsTheUserIsMember = @user.groups.pluck(:id)
end

def update
  @user = User.find_by(id: params[:id])
  @user.memberships.destroy_all
  #params includes the list of group_ids that will be associated to the user
  params[:group_ids].each do |group_id|
    membership=Membership.create(user_id: @user.id, group_id: group_id)
  end
end

GroupsController

def edit
  @group = Group.find_by(id: params[:id])
  @allUsers = User.all
  @usersTheGroupIncludes = @group.users.pluck(:id)
end

def update
  @group = Group.find_by(id: params[:id])
  @group.memberships.destroy_all
  #params includes the list of user_ids that will be associated to the group
  params[:user_ids].each do |user_id|
    membership=Membership.create(user_id: user_id, group_id: @group.id)
  end
end

视图/用户/ edit.html.erb

<%= simple_form_for(@user) do |f| %>
  <h2>Groups</h2>
  <p>Select the groups where the user is member</p>
  <table>
    <%= render 'groups/groups_header' %>
    <%= render partial: 'groups/groups', collection: @allGroups, as: :group, locals: { user_id: @user.id } %>
  </table>
  <%= f.submit "Update", class: "btn" %>
<% end %>

视图/组/ edit.html.erb

<%= simple_form_for(@group) do |f| %>
  <h2>Users</h2>
  <p>Select the users that belong to the group</p>
  <table>
    <%= render 'users/users_header' %>
    <%= render partial: 'users/users', collection: @allUsers, as: :user, locals: { group: @group.id } %>
  </table>
  <%= f.submit "Update", class: "btn" %>
<% end %>

视图/组/ _groups.html.erb

<tr>
  <td>
    <!-- check box to include or remove the group -->
    <% if @usersTheGroupIncludes.include?(user_id) %>
      <%= check_box_tag 'user_ids[]', user_id, checked: true %>
    <% else %>
      <%= check_box_tag 'user_ids[]', user_id %>
    <% end %>
  </td>
  <td>Group: <%= group.name %></td>
</tr>

视图/用户/ _users.html.erb

<tr>
  <td>
    <!-- check box to include or remove the user -->
    <% if @groupsTheUserIsMember.include?(group_id) %>
      <%= check_box_tag 'group_ids[]', group_id, checked: true %>
    <% else %>
      <%= check_box_tag 'group_ids[]', group_id %>
    <% end %>
  </td>
  <td>User: <%= user.name %></td>
</tr>