- Dealing with has_many through checkboxes in a nested form
- = semantic_form_for @raduser do |f|
- %ul
- %li
- = f.text_field :expiration, :placeholder => 'Expiration Date', :id => 'expiration_date'
- - f.fields_for :radcheck do |builder|
- = builder.text_field :username, :placeholder => 'Stuff'
- -Radgroup.all.each do |group|
- %li
- = check_box_tag 'radcheck[radgroup_ids]', group.id, @radcheck.radgroup_ids.include?(group.id)
- = group.groupname
- %li
- = f.submit
- undefined method `radgroup_ids' for #<ActiveRecord::Relation:0x007faaa24c2468>
- class Raduser < ActiveRecord::Base
- has_many :radcheck, :dependent => :destroy
- accepts_nested_attributes_for :radcheck, :reject_if => lambda { |a| a[:value].blank? }, :allow_destroy => true
- end
- class Radcheck < ActiveRecord::Base
- self.table_name = 'radcheck'
- attr_accessible :attribute_name, :username, :value, :op, :radgroupcheck_id, :radcheck_serial, :radgroup_ids
- belongs_to :raduser
- has_many :radusergroup, :dependent => :destroy
- has_many :radgroup, :through => :radusergroup
- end
- class Radgroup < ActiveRecord::Base
- self.table_name = 'radgroup'
- has_many :radusergroup, :dependent => :destroy
- has_many :radcheck, :through => :radusergroup
- end
- class Radusergroup < ActiveRecord::Base
- belongs_to :radcheck
- belongs_to :radgroup
- end
- def new
- @radcheck = Radcheck.new
- ...
- end
- def create
- @radcheck = Radcheck.new(params[:radcheck])
- ...
- end
- +----------------+-----------------------+----------+------+
- | radcheck_id | radgroup_id | priority | id |
- +----------------+-----------------------+----------+------+
- | 8 | 3 | 1 | 1 |
- | 4 | 9 | 0 | 2 |
- | 22 | 4 | 1 | 3 |
- | 2 | 6 | 1 | 4 |
- +----------------+-----------------------+----------+------+
- | ...username | --groupname | priority | id |
- +----------------+-----------------------+----------+------+
- | user123344 | group88888 | 1 | 1 |
- | shushQb | 30-minutes | 0 | 2 |
- | forty | gigiig | 1 | 3 |
- | snang | ps-staff | 1 | 4 |
- def radgroup_ids
- radgroups.map(&:id)
- end
- @radcheck.radgroups.include?(group)