Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: Manipulating a Join Table in a Nested Form
    primarykey
    data
    text
    <p>I have two models connected by a join table:</p> <pre><code>class Publication &lt; ActiveRecord::Base attr_accessible :title, :author_attributes, :translator_attributes has_many :publication_contributors has_many :authors, :through =&gt; :publication_contributors, :source =&gt; :contributor, :conditions =&gt; {:publication_contributors =&gt; {:contributor_type =&gt; "Author"}} has_many :translators, :through =&gt; :publication_contributors, :source =&gt; :contributor, :conditions =&gt; {:publication_contributors =&gt; {:contributor_type =&gt; "Translator"}} accepts_nested_attributes_for :authors, :translators end class Contributor &lt; ActiveRecord::Base attr_accessible :name has_many :publications, :through =&gt; :publication_contributors has_many :publication_contributors end class PublicationContributor &lt; ActiveRecord::Base attr_accessible :contributor_type belongs_to :publication belongs_to :contributor end </code></pre> <p>Note that the join table has a third attribute (beside publication_id and contributor_id), which is called contributor_type. This attribute might contain a role such as "Author", "Translator", "Editor", or something else. In my Publication model, I created a pair of associations for two of the more common contributor_types, "Author" and "Translator". These associations work well at retrieving the relevant data, such as with <code>@publication.authors</code>. It sputters when creating these associations through a nested form, however.</p> <p>My form looks something like this:</p> <pre><code>&lt;%= form_for @publication, :remote =&gt; true do |f| %&gt; &lt;%= f.label :title %&gt;: &lt;%= f.text_field :title %&gt; &lt;%= f.fields_for :authors do |builder| %&gt; &lt;%= builder.label :name, "Author" %&gt;: &lt;%= builder.text_field :name %&gt; &lt;% end %&gt; &lt;%= f.fields_for :translators do |builder| %&gt; &lt;%= builder.label :name, "Translator" %&gt;: &lt;%= builder.text_field :name %&gt; &lt;% end %&gt; &lt;%= f.submit %&gt; &lt;% end %&gt; </code></pre> <p>In my controller:</p> <pre><code>def create publication = Publication.create(params[:publication]) end </code></pre> <p>The form renders as expected, but it sputters during the create action. I had hoped that Rails would know to magically assign the proper contributor_type based on the conditions in the Publication associations, such as:</p> <pre><code>has_many :authors, :through =&gt; :publication_contributors, :source =&gt; :contributor, :conditions =&gt; {:publication_contributors =&gt; {:contributor_type =&gt; "Author"}} </code></pre> <p>Unfortunately, it does not. I get this error during creation:</p> <pre><code>Mysql2::Error: Column 'contributor_type' cannot be null </code></pre> <p>This makes me think that my only recourse is to manually assign the contributor_type in a <code>before_create</code> callback, but if that's the case, how do I determine which type to assign? The parameters have the proper type in their name, for instance:</p> <pre><code>publication[authors_attributes][0][name] </code></pre> <p>Is there some way to access that information in the model layer?</p> <p>UPDATE:</p> <p>My <code>new</code> action:</p> <pre><code>def new @publication = Publication.new publication_contributor = @publication.publication_contributors.build contributor = publication_contributor.contributor.build end </code></pre> <p>It throws this error on the final line (the one setting <code>contributor</code>):</p> <pre><code>undefined method `build' for nil:NilClass </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload