0

I need some conceptual help:
Assume your Users are essentially a business. You have Employees and you have Staff Positions. Essentially, one employee could hold multiple positions and one position could hold multiple employees.
My have_many :through is working between the Employees and the Positions through a join table Staffization. However, my edit form for the employee is returning ALL the Positions as checkboxes for the whole app, not just the ones for this particular User. And, none are being saved when I submit an update. Do I need to do something different with my associations, or is there a better way to narrow the data in the forms?
My models:

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

My employee edit fields form is set up to return checkboxes for the possible positions the employee could hold but is return all the positions in the whole app and is not updating the data when I hit submit:

 - 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

My employees controller update def added the line for the have_many :through association. Is this where I should narrow the return down to the current signed in user's employees and positions?

@employee.attributes = {'position_ids' => []}.merge(params[:employee] || {})
4

3 回答 3

2

首先,您不应该使用:

class Employee
  has_and_belongs_to_many :positions
end

class Position
  has_and_belongs_to_many :employees
end

然后,您可以使用以下方法缩小可用职位:

Position.where(:user_id => @employee.user_id).each # etc.

你甚至可以为它创建一个范围:

class Position
  def available_for_employee employee
    where(:user_id => employee.user_id)
  end
end

...然后在生成复选框的助手中使用它

def position_checkboxes_for_employee employee
  Position.available_for_employee(employee).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
  end
end
于 2011-09-04T15:44:55.307 回答
1

将所有位置作为复选框返回正是您想要的,不是吗?如果员工换了职位怎么办?那么你需要那个复选框,而不仅仅是选中的那些..

于 2011-09-02T00:10:13.393 回答
0

感谢一位朋友,因为我的员工和职位之间的have_many属于企业。我需要将 attr_accessible position_ids 和 attr_accessible employee_ids 添加到各自的模型中。此外,在我的员工视图字段中,我需要添加选项,以便我的职位调用仅调用与该业务相关的那些职位,如下所示:

  - Position.find_all_by_user_id(@employee.user_id).each do |position|
   = check_box_tag :position_ids, position.id, @employee.positions.include?(position), :name => 'employee[position_ids][]'
   = label_tag :position_ids, position.position_title
于 2011-09-04T15:18:42.877 回答