ruby-on-rails - Rails 更新三向连接表

是否有“Rails 方式”来更新单个表单上的三列连接表?我有三个模型,UserLocationRole 和一个三列连接表 UserLocationRole:

class User < ActiveRecord::Base
  belongs_to :company
  has_many :user_location_roles
  has_many :locations, through: :user_location_roles
  has_many :roles, through: :user_location_roles
end

class Location < ActiveRecord::Base
  # has_many through etc
end 

class Role < ActiveRecord::Base
  # has_many through etc
end

class UserLocationRole < ActiveRecord::Base
  belongs_to :user
  belongs_to :location
  belongs_to :role
end

我想要在新建/编辑用户表单上看到一个复选框列表:

1. Location: ABC
   [] Role 1
   [] Role 2
   [] Role 3
2. Location: DEF
   [] Role 1
   [] Role 2
   [] Role 3

在我的用例中位置和角色的数量很少(通常是 2 或 3 个位置和 3 个角色),所以这个表单永远不会太大/太尴尬。我特别想列出每个位置的所有角色,即我不希望角色直接连接到位置(角色表中没有 location_id 列)。

我创建了一个感觉非常“非 Railsy”的解决方案,并且想知道是否有更简洁的方法来使用例如accepts_nested_attributes_for

我的解决方案: 用户 Controller :

def edit
  @user = User.find(params[:id])
  @permissions = @user.user_location_roles # used to make sure appropriate checkboxes are ticked on the form.
end

def create
  @user = User.new(user_params)
  @user.set_permissions(params[:user_permissions])
  @user.save #etc
end

def update
  @user = User.find(params[:id])
  @user.set_permissions(params[:user_permissions])
  @user.update #etc
end

我的表单遍历所有可能的位置,然后为每个可能的角色打印一个复选框。它检查该特定记录是否已存在于连接表中,如果存在,则将“checked”标志设置为 true。

<% @user.company.locations.all.each do |location| %>
  <p><%= location.name %></p>
  <% @user.company.roles.all.each do |role| %>
    <% checked = (@permissions) ? (@permissions.select { |p| p.location == location and p.role == role  }.any?) : false %>
    <label><%= check_box_tag "user_permissions[#{location.id}][#{role.id}]", "1", checked %><%= role.name %></label><br> 
  <% end %>
<% end %>

我的用户模型然后从表单中获取后续哈希,删除该用户的连接表中的所有条目(非常丑陋),然后根据需要创建新条目。

def set_permissions(permissions_hash)
  self.user_location_roles.destroy_all
  permissions_hash.each do |location_id, roles_hash|
    roles_hash.each do |role_id, bool|
      self.user_location_roles.build(location_id: location_id, role_id: role_id)
    end
  end
end

请帮助我将代码重构为更简洁的代码。至少,如果提交的用户表单没有更改权限,我不需要删除所有权限并重新创建它们。理想情况下,还有一种更简洁的方法来生成表单输出。谢谢。

最佳答案

您可以使用 https://github.com/ryanb/nested_form以单一形式更新嵌套对象。

https://stackoverflow.com/questions/27396459/

相关文章:

java - 从java中的单独文件夹中读取文本文件

java - spring @RequestParam Unicode 不正确

java - slf4j + log4j 显示除我的以外的所有日志

java - JXLayer 实现

python - 获取每个 json 模式错误的属性

java - 如何组织包之间有空白的导入?

tfs - 使用 Team Build 2013 拒绝门控 checkin

c# - 防止标签点击事件作为默认行为

regex - 为什么显示此错误 "Values must match the following

haskell - 用 GHC 将小型 Haskell 程序编译成巨大的二进制文件