Ruby on rails Rails 3有很多:通过概念
我需要一些概念上的帮助:Ruby on rails Rails 3有很多:通过概念,ruby-on-rails,model,model-associations,Ruby On Rails,Model,Model Associations,我需要一些概念上的帮助: 假设您的用户本质上是一个企业。你有员工,你有员工职位。基本上,一个员工可以担任多个职位,一个职位可以担任多个员工。 我的have_mu many:through是通过一个连接表标记在员工和职位之间工作。但是,我的员工编辑表单将所有职位作为整个应用程序的复选框返回,而不仅仅是这个特定用户的职位。而且,当我提交更新时,没有保存任何内容。 我是否需要对我的关联做一些不同的事情,或者是否有更好的方法来缩小表单中的数据范围? 我的模型: class User < Activ
假设您的用户本质上是一个企业。你有员工,你有员工职位。基本上,一个员工可以担任多个职位,一个职位可以担任多个员工。
我的have_mu many:through是通过一个连接表标记在员工和职位之间工作。但是,我的员工编辑表单将所有职位作为整个应用程序的复选框返回,而不仅仅是这个特定用户的职位。而且,当我提交更新时,没有保存任何内容。 我是否需要对我的关联做一些不同的事情,或者是否有更好的方法来缩小表单中的数据范围?
我的模型:
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
我的员工控制器更新def为have_many:through关联添加了一行。我是否应该在这里将返回范围缩小到当前登录用户的员工和职位
@employee.attributes = {'position_ids' => []}.merge(params[:employee] || {})
将所有位置作为复选框返回正是您想要的,不是吗?
如果员工换了职位怎么办?那么你就需要那个复选框,而不仅仅是那些选中的 将所有位置作为复选框返回正是您想要的,不是吗?
如果员工换了职位怎么办?那么你就需要那个复选框,而不仅仅是那些选中的 多亏了一位朋友,因为我的员工和职位之间有很多沟通。我需要将attr\u-accessible-position\u id和attr\u-accessible-employee\u id添加到相应的模型中。此外,在“我的员工”视图字段中,我需要添加选项,以便我的职位召唤仅调用与此业务相关的职位,如下所示:
- 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
多亏了一位朋友,因为我在我的员工和职位之间有很多沟通。我需要将attr\u-accessible-position\u id和attr\u-accessible-employee\u id添加到相应的模型中。此外,在“我的员工”视图字段中,我需要添加选项,以便我的职位召唤仅调用与此业务相关的职位,如下所示:
- 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
首先,您不应该使用:
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
首先,您不应该使用:
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
谢谢你的回复!。。。此用户的所有复选框都很好。但是,应用程序中每个用户的所有复选框都不好。我想知道我的问题是否在员工控制器中,我在更新定义中添加了上面的行。我不确定是否需要在某个地方输入当前用户的id。谢谢您的回复!。。。此用户的所有复选框都很好。但是,应用程序中每个用户的所有复选框都不好。我想知道我的问题是否在员工控制器中,我在更新定义中添加了上面的行。我不确定是否需要将当前用户的id传递到某个位置。是的,谢谢,这也很有效…has_many通过has_和Own_Owns_to_many的好处是该关系的联接表允许我在将来添加其他属性。是的,谢谢,这也很有效……has_many通过has_和_own_to_many的好处是关系的联接表,它允许我在将来添加其他属性。