After upgrading to Rails 4.1, new polymorphic associations are invalid when saving them along with their parent

has-many-through, polymorphic-associations, ruby-on-rails, ruby-on-rails-4

Solution

In Rails 3.2 owner model has been saved before perform validation nested association, in 4.1 validation before model saved, and because post not saved, validation

class ContactPublishment
    validates_presence_of :publishable

does not allow to pass validation (post not saved in db)

For resolve this, you may disable validation in Post model, (validation on ContactPublishment has been called from Contact model)

class Post < ActiveRecord::Base
      has_many :contact_publishments, as: :publishable, validate: false

or replace presence validation like this:

class ContactPublishment < ActiveRecord::Base
  validates_associated :publishable

change_column :contact_publishments, :publishable_type, :string, null: false
change_column :contact_publishments, :publishable_id, :integer, null: false

or do it through proxy_association

Problem

After upgrading from Rails 3.2 to 4.1, the following code which used to work is now failing: in a controller/spec: ``` post = user.posts.build post.contacts << contact # contact is a persisted record post.save! # now fails ``` I'm basically trying to save the post along with its associated contact, which is supposed to create a `contact_publishment` record on-the-fly. The error is on the new `contact_publishment` record: "Publishable can't be blank" the model: ``` class Contact ... has_many :contact_publishments ... end class ContactPublishment ... belongs_to :publishable, polymorphic: true belongs_to :contact validates_uniqueness_of :publishable_id, :scope => [:contact_id, :publishable_type] validates_presence_of :contact, :publishable ... end class Post ... has_many :contact_publishments, as: :publishable has_many :contacts, through: :contact_publishments ... end ```

Original source