This project is archived and is in readonly mode.
has_many :through Doesn't Use Conditions For Creating New Records
Reported by Matt Bauer | February 27th, 2009 @ 12:20 AM | in 2.x
has_many :through Doesn't Use Conditions For Creating New Records. Assuming the following ActiveRecord:
class Post < ActiveRecord::Base
has_many :skimmers, :class_name => 'Reader', :conditions => { :skimmer => true }
has_many :impatient_people, :through => :skimmers, :source => :person
end
When a new record is created with the following code:
my_post.impatient_people.create!(:first_name => 'bob')
Creates the proper models in the readers and people tables but the record in the readers table does not have the skimmer attribute set to true. The attached patch will interrogate the conditions options and if it's a hash, add it to the attribute hash when creating the join model.
Comments and changes to this ticket
-
Matt Bauer February 27th, 2009 @ 12:24 AM
- no changes were found...
-
Josh Susser March 3rd, 2009 @ 03:59 AM
- Tag changed from activerecord, has_many_through to activerecord, has_many_through, patch
While it's possible to create sets of conditions where this will work, you can also easily create ones where they don't. Just try an inequality test and you'll see. e.g. ["published_at < ?", 1.year.ago]. I think coming up with a way to do the right thing while avoiding surprises is going to be very difficult.
-
Matt Bauer March 3rd, 2009 @ 04:10 AM
I completely agree with you which is why this patch only works when there is a hash provided. This will avoid surprises though I agree it may be to subtle yet.
-
Duncan McKee March 3rd, 2009 @ 05:49 PM
+1 — As it is now, the create method on a has_many association does respect conditions as long as they are in hash form, and it is documented as such. The surprise is that, using the example above, post.skimmers.create creates a reader with skimmer==true, while post.impatient_people.<< creates a reader with skimmer==nil.
-
Pratik June 21st, 2009 @ 05:43 PM
- State changed from new to resolved
Resolved in 83c1934003740ed01c618a8943457a0df53e2adb
-
Repository June 21st, 2009 @ 09:25 PM
(from [83c1934003740ed01c618a8943457a0df53e2adb]) Ensure hm:t#create respects source associations hash conditions [#2090 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/83c1934003740ed01c618a8943457a... -
Repository June 23rd, 2009 @ 10:50 PM
(from [1707cd907fdc14e69ddee7da3909f077c7621bcd]) Ensure hm:t#create respects source associations hash conditions [#2090 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/1707cd907fdc14e69ddee7da3909f0...
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>
People watching this ticket
Attachments
Referenced by
- 2090 has_many :through Doesn't Use Conditions For Creating New Records (from [83c1934003740ed01c618a8943457a0df53e2adb]) Ensure ...
- 2090 has_many :through Doesn't Use Conditions For Creating New Records (from [1707cd907fdc14e69ddee7da3909f077c7621bcd]) Ensure ...