Rails 3 has_many:通过概念
我需要一些概念上的帮助:
假设您的用户本质上是一家企业。您有员工,也有员工职位。本质上,一名员工可以担任多个职位,一个职位可以担任多名员工。
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您不应该使用 :
然后,您可以使用 : 缩小可用位置,
您甚至可以为其设定一个范围:
...然后在生成复选框的帮助程序中使用它
First, shouldn't you be using :
then, you could narrow the available positions with :
you could even make a scope for it:
... and then use this in a helper that generates your checkboxes
将所有位置作为复选框返回正是您想要的,不是吗?
如果员工调换职位怎么办?那么你需要那个复选框,而不仅仅是选中的复选框。
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..
感谢一位朋友,因为我的员工和职位之间有很多联系,属于企业。我需要将 attr_accessibleposition_ids 和 attr_accessibleemployee_ids 添加到相应的模型中。此外,在我的员工视图字段中,我需要添加选项,以便我的职位调用仅调用与此业务相关的职位,如下所示:
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: