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