Rails 3 has_many:通过概念

发布于 2024-12-02 15:15:55 字数 1351 浏览 0 评论 0原文

我需要一些概念上的帮助:
假设您的用户本质上是一家企业。您有员工,也有员工职位。本质上,一名员工可以担任多个职位,一个职位可以担任多名员工。
我的 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

我的员工控制器更新 def 添加了have_many :through 关联的行。这是我应该将返回范围缩小到当前登录用户的员工和职位的地方吗?

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

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] || {})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作死小能手 2024-12-09 15:15:55

首先,您不应该使用 :

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

First, shouldn't you be using :

class Employee
  has_and_belongs_to_many :positions
end

class Position
  has_and_belongs_to_many :employees
end

then, you could narrow the available positions with :

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

you could even make a scope for it:

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

... and then use this in a helper that generates your checkboxes

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
森林很绿却致人迷途 2024-12-09 15:15:55

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

returning ALL the positions as checkboxes is exactly what you'd want, no?
what if a employee changes positions? you'd need that checkbox then, not only the checked ones..

赠意 2024-12-09 15:15:55

感谢一位朋友,因为我的员工和职位之间有很多联系,属于企业。我需要将 attr_accessibleposition_ids 和 attr_accessibleemployee_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

Thanks to a friend, since my have_many through between my employees and positions belongs to the business. I needed to add the attr_accessible position_ids and attr_accessible employee_ids to the respective models. In addition, in my employee view field, I needed to add the options so that my call for positions only calls those positions associated with this business, like so:

  - 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文