【发布时间】:2011-09-01 14:36:09
【问题描述】:
我需要一些概念上的帮助:
假设您的用户本质上是一家企业。您有员工,也有员工职位。本质上,一名员工可以担任多个职位,一个职位可以担任多个员工。
我的 have_many :through 正在通过连接表 Staffization 在员工和职位之间工作。但是,我的员工编辑表单将所有职位作为整个应用程序的复选框返回,而不仅仅是这个特定用户的复选框。而且,当我提交更新时,没有一个被保存。
我是否需要对我的关联做一些不同的事情,还是有更好的方法来缩小表单中的数据范围?
我的模型:
class User < ActiveRecord::Base
has_many :employees, :dependent => :destroy
has_many :positions, :dependent => :destroy
class Employee < ActiveRecord::Base
belongs_to :user
has_many :positions, :through => :staffizations
has_many :staffizations, :dependent => :destroy
class Position < ActiveRecord::Base
belongs_to :user
has_many :employees, :through => :staffizations
has_many :staffizations, :dependent => :destroy
class Staffization < ActiveRecord::Base
belongs_to :employee
belongs_to :position
我的员工编辑字段表单设置为返回员工可能持有的职位的复选框,但返回整个应用程序中的所有职位,并且在我点击提交时不更新数据:
- Position.all.each do |position|
= check_box_tag :position_ids, position.position_name, @employee.positions.include?(position), :name => 'employee[position_ids][]'
= label_tag :position_ids, position.position_name
我的员工控制器更新定义为 have_many :through 关联添加了行。这是我应该将回报缩小到当前登录用户的员工和职位的地方吗?
@employee.attributes = {'position_ids' => []}.merge(params[:employee] || {})
【问题讨论】:
标签: ruby-on-rails model model-associations