Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

has_many :through associations are created with .create! even if the parent is created with .create

#6217

When saving a model with a has_many :through association using .create, a validation exception can be raised when none is expected.

class Address < ActiveRecord::Base
  belongs_to :user

  def validate
    errors.add :user, "This is a test."
  end
end

class User < ActiveRecord::Base
  has_many :addresses, :validate => true
  has_many :admins, :through => :addresses, :source => :user
end

# (BUG) this raises a ActiveRecord::RecordInvalid (and I did not expect it to since I used .create)
User.create(:admins => [User.find(1)])

# (OK) this raises a ActiveRecord::RecordInvalid (and I expect it to since I used .create!)
User.create!(:addresses => [Address.find(1)])

# (OK) this does not raise anything (and I did not expect it to since I used .create)
User.create(:addresses => [Address.find(1)])

I'm using Rails 2.3.10, but I believe the same applies to Rails 3.

Reported by Greg Hazel · December 23rd, 2010 @ 11:10 AM

State: open
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    March 24th, 2011 @ 12:00 AM

  2. rails
    rails
    • State changed from open to stale

    March 24th, 2011 @ 12:00 AM

  3. Greg Hazel
    Greg Hazel
    • State changed from stale to open

    This bug still exists. [state:open]

    March 24th, 2011 @ 12:01 AM

  4. ronin-23439 (at lighthouseapp)
    ronin-23439 (at lighthouseapp)

    +1 this problem also exists when using the << method.

    March 29th, 2011 @ 02:20 AM

  5. Erik Michaels-Ober
    Erik Michaels-Ober

    +1

    This is causing an issue in RailsAdmin.

    March 29th, 2011 @ 04:52 AM

  6. hemant
    hemant
    • Tag changed from activerecord, has_many_through, validation to :confirm, activerecord, has_many_through, testcase, validation

    +1 The issue exists in edge rails and all previous versions.
    I've attached a failing test.

    I see that in activerecord/lib/active_record/associations/has_many_through_association.rb, line 36

          def insert_record(record, validate = true)
            ensure_not_nested
            return if record.new_record? && !record.save(:validate => validate)
    
            through_record(record).save!
            update_counter(1)
            record
          end
    

    through_record(join record) is explicitly being saved with save! which will always result in an exception if record not saved.
    Shouldn't there be a way we can track if we need to use a bang operator here or not and based on that either add error to the parent record or raise exception.

    May 13th, 2011 @ 10:16 AM