I have the current model relationship:
class Group < ActiveRecord::Base
has_many :group_memberships, dependent: :destroy
has_many :subjects, through: :group_memberships
has_many :entries, through: :group_memberships
accepts_nested_attributes_for :group_memberships, allow_destroy: true
class GroupMembership < ActiveRecord::Base
belongs_to :group
belongs_to :subject
belongs_to :entry
class Issue < ActiveRecord::Base
has_many :group_memberships, through: :entries, dependent: :destroy
has_many :groups, through: :group_memberships, dependent: :destroy
accepts_nested_attributes_for :group_memberships, allow_destroy: true, reject_if: :all_blank
accepts_nested_attributes_for :groups, allow_destroy: true
With cocoon nested fields and simpleform - when I add or update existing objects - two incomplete group_membership records are created (i.e they are splitting the required foreign-keys over two models.
The only difference between this and the associations I normally use is the join table in this case has three foreign keys rather than two.
The SQL queries:
INSERT INTO `groups` (`name`, `created_at`, `updated_at`) VALUES ('test group', '2016-01-11 15:53:22', '2016-01-11 15:53:22')
INSERT INTO `group_memberships` (`subject_id`, `group_id`, `created_at`, `updated_at`) VALUES (16, 18, '2016-01-11 15:53:22', '2016-01-11 15:53:22')
INSERT INTO `group_memberships` (`entry_id`, `group_id`, `created_at`, `updated_at`) VALUES (20, 18, '2016-01-11 15:53:22', '2016-01-11 15:53:22')
The parameters in the controller seem to be normal:
"groups_attributes"=>{"1452527593321"=>{"name"=>"test group", "subject_ids"=>["", "16"], "_destroy"=>"false"}}, "_destroy"=>"false", "id"=>"20"}
I really am a bit lost as to why I am dealing with duplicated incomplete queries when the parameters appear fine
The form itself looks like below:
<div id="groups">
<div id="groups_from_list">
<%= f.simple_fields_for :groups do |group| %>
<%= render 'group_fields', :f => group %>
<% end %>
</div>
<%= link_to_add_association 'add a group', f, :groups, :class => 'btn btn-primary btn' %>
</div>
I have not had any problems with standard has_many through associations using nested forms - but I assume having the three foreign keys in the group_membership join table is causing the problem.
Aucun commentaire:
Enregistrer un commentaire