【问题标题】:Rails 3 has_many :through conceptRails 3 has_many:通过概念
【发布时间】: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


    【解决方案1】:

    首先,您不应该使用:

    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
    

    【讨论】:

    • 是的,谢谢,这也很有效...通过 has_and_belongs_to_many 对 has_many 的好处是关系的连接表,允许我在未来添加其他属性。
    【解决方案2】:

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

    【讨论】:

    • 感谢您的回复!...该用户的所有复选框都很好。但是,应用程序中每个用户的所有复选框都是错误的。我想知道我的问题是否出在员工控制器中,我在更新定义中添加了上面的行。我不确定是否需要在某处传递当前用户的 id。
    【解决方案3】:

    感谢一位朋友,因为我的员工和职位之间的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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多