I’m trying to add has_many_through relationship through a form nested in a parent model. I can’t figure out if this is possible?
The nested form works ok without the checkboxes. And the checkboxes work fine when they’re outside the 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
This throws an error:
undefined method `radgroup_ids' for #<ActiveRecord::Relation:0x007faaa24c2468>
I’ve got the models set up fine I think as all values save perfectly without the checkboxes
— EDIT ADDED MODELS —
Radusers have many Radcheck (singular due to legacy db). Radcheck has many radgroups through radusergroup.
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
— UPDATE TWO —
def new
@radcheck = Radcheck.new
...
end
def create
@radcheck = Radcheck.new(params[:radcheck])
...
end
— UPDATE 3 —
My db currently looks like this:
+----------------+-----------------------+----------+------+
| radcheck_id | radgroup_id | priority | id |
+----------------+-----------------------+----------+------+
| 8 | 3 | 1 | 1 |
| 4 | 9 | 0 | 2 |
| 22 | 4 | 1 | 3 |
| 2 | 6 | 1 | 4 |
I have the solution suggested below working.
What I was hoping to do was insert the radcheck_username and radgroup_groupname into the db, not the radcheck_id:
+----------------+-----------------------+----------+------+
| ...username | --groupname | priority | id |
+----------------+-----------------------+----------+------+
| user123344 | group88888 | 1 | 1 |
| shushQb | 30-minutes | 0 | 2 |
| forty | gigiig | 1 | 3 |
| snang | ps-staff | 1 | 4 |
Is this possible?
The problem appears to be that you’re trying to access Radcheck’s various Radgroups through a method that isn’t defined yet. I can see a couple of solutions to this. One would be to add the missing radgroup_ids method to your model. Alternatively, I think you could call include? on Radchecks radgroups relation.
Solution A
Within your Radcheck class, define:
Solution B
Make a minor change to the way you’re checking for included Radgroups