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
Reported by Greg Hazel | December 23rd, 2010 @ 11:10 AM
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.
Comments and changes to this ticket
-
rails March 24th, 2011 @ 12:00 AM
- 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.
-
rails March 24th, 2011 @ 12:00 AM
- State changed from open to stale
-
Greg Hazel March 24th, 2011 @ 12:01 AM
- State changed from stale to open
This bug still exists. [state:open]
-
ronin-23439 (at lighthouseapp) March 29th, 2011 @ 02:20 AM
+1 this problem also exists when using the << method.
-
hemant May 13th, 2011 @ 10:16 AM
- 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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>