Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9270651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:25:22+00:00 2026-06-18T15:25:22+00:00

I have two models connected by a join table: class Publication < ActiveRecord::Base attr_accessible

  • 0

I have two models connected by a join table:

class Publication < ActiveRecord::Base
  attr_accessible :title, :author_attributes, :translator_attributes

  has_many :publication_contributors
  has_many :authors, :through => :publication_contributors, :source => :contributor,
    :conditions => {:publication_contributors => {:contributor_type => "Author"}}
  has_many :translators, :through => :publication_contributors, :source => :contributor,
    :conditions => {:publication_contributors => {:contributor_type => "Translator"}}

  accepts_nested_attributes_for :authors, :translators
end

class Contributor < ActiveRecord::Base
  attr_accessible :name

  has_many :publications, :through => :publication_contributors
  has_many :publication_contributors
end

class PublicationContributor < ActiveRecord::Base
  attr_accessible :contributor_type

  belongs_to :publication
  belongs_to :contributor
end

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 @publication.authors. It sputters when creating these associations through a nested form, however.

My form looks something like this:

<%= form_for @publication, :remote => true do |f| %>

  <%= f.label :title %>:
  <%= f.text_field :title %>

  <%= f.fields_for :authors do |builder| %>
    <%= builder.label :name, "Author" %>:
    <%= builder.text_field :name %>
  <% end %>

  <%= f.fields_for :translators do |builder| %>
    <%= builder.label :name, "Translator" %>:
    <%= builder.text_field :name %>
  <% end %>

  <%= f.submit %>
<% end %>

In my controller:

def create
  publication = Publication.create(params[:publication])
end

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:

has_many :authors, :through => :publication_contributors, :source => :contributor,
:conditions => {:publication_contributors => {:contributor_type => "Author"}}

Unfortunately, it does not. I get this error during creation:

Mysql2::Error: Column 'contributor_type' cannot be null

This makes me think that my only recourse is to manually assign the contributor_type in a before_create 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:

publication[authors_attributes][0][name]

Is there some way to access that information in the model layer?

UPDATE:

My new action:

def new
  @publication = Publication.new
  publication_contributor = @publication.publication_contributors.build
  contributor = publication_contributor.contributor.build
end

It throws this error on the final line (the one setting contributor):

undefined method `build' for nil:NilClass
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:25:24+00:00Added an answer on June 18, 2026 at 3:25 pm

    Can you try the following?

    class Publication < ActiveRecord::Base
      attr_accessible :title, :publication_contributors_attributes
    
      has_many :publication_contributors
      has_many :authors, through: :publication_contributors, source: :contributor, conditions: { publication_contributors: { contributor_type: 'Author' } }
      has_many :translators, through: :publication_contributors, source: :contributor, conditions: { publication_contributors: { contributor_type: 'Translator' } }
    
      accepts_nested_attributes_for :publication_contributors
    end
    
    class Contributor < ActiveRecord::Base
      attr_accessible :name
    
      has_many :publications, through: :publication_contributors
      has_many :publication_contributors
    end
    
    class PublicationContributor < ActiveRecord::Base
      attr_accessible :contributor_type
    
      belongs_to :publication
      belongs_to :contributor
    
      accepts_nested_attributes_for :contributor
    end
    
    <%= form_for @publication, :remote => true do |f| %>
    
      <%= f.label :title %>:
      <%= f.text_field :title %>
    
      <%= f.fields_for :publication_contributors do |pc_form| %>
        <%= pc_form.hidden_field :contributor_type %>
        <%= pc_form.fields_for :contributor do |c_form| %>
          <%= c_form.label :name, "Author" %>:
          <%= c_form.text_field :name %>
        <% end %>
      <% end %>
    
      <%= f.submit %>
    <% end %>
    

    UPDATE: controller code

    @publication.publication_contributors.build
    @publication.publication_contributors.each do |pc|
      pc.build_contributor
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, Landscape: class Landscape < ActiveRecord::Base has_many :images, :as => :imageable
I have two models, Worker and Project, and they are connected with has_many through
I have two models: User and Car with the following associations: User has_many Car
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models like this: class ClassA(models.Model): ida = models.AutoField(primary_key=True) classb = models.ForeignKey(ClassB)
I have two models, user and group. I also have a joining table groups_users.
I have two models, a Project and an Action: class Project(models.Model): name = models.CharField(Project
How to add an computed column to the model class. I have two models
I have two tables and a join table. I tried using the $hasAndBelongsToMany but
I have two models: User (pre-defined by Django) and UserProfile that are connected through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.