概念的な助けが必要です:
ユーザーが本質的にビジネスであると仮定します。従業員がいて、スタッフの役職があります。基本的に、1 人の従業員が複数の役職に就くことができ、1 つの役職に複数の従業員が所属することができます。
私の have_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
私の従業員編集フィールド フォームは、従業員が保持できる可能性のあるポジションのチェックボックスを返すように設定されていますが、アプリ全体のすべてのポジションを返し、送信を押したときにデータを更新していません。
- 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] || {})