This project is archived and is in readonly mode.
Association extensions and scopes on the associated model behave inconsistantly
-
Brian Terlson
I cannot recreate this behavior in 2.2.0 RC, so I'd say this ticket can be closed.
-
Frederick Cheung
- State changed from new to resolved
-
Mani Tadayon
- Importance changed from to
I am encountering this exact same bug on Rails 2.3.8!
I created a blank new rails app using the example code above, with one tiny change to the Blog class to help with debugging:
class Blog < ActiveRecord::Base has_many :entries do def print each { |e| puts e.status } # print the status end end endHere is what happens in the console:
>> Rails.version "2.3.8" >> b = Blog.first #<Blog:0x2397edc> { :id => 1, :created_at => Thu, 19 Aug 2010 19:36:44 UTC +00:00, :updated_at => Thu, 19 Aug 2010 19:36:44 UTC +00:00 } >> b.entries [ [0] #<Entry:0x23767b4> { :id => 1, :status => "active", :blog_id => 1, :created_at => Thu, 19 Aug 2010 19:37:02 UTC +00:00, :updated_at => Thu, 19 Aug 2010 19:37:02 UTC +00:00 }, [1] #<Entry:0x2376750> { :id => 2, :status => "active", :blog_id => 1, :created_at => Thu, 19 Aug 2010 19:37:04 UTC +00:00, :updated_at => Thu, 19 Aug 2010 19:37:04 UTC +00:00 }, [2] #<Entry:0x2376700> { :id => 3, :status => "inactive", :blog_id => 1, :created_at => Thu, 19 Aug 2010 19:37:06 UTC +00:00, :updated_at => Thu, 19 Aug 2010 19:37:06 UTC +00:00 } ] >> b.entries.active.print active active inactive # this shouldn't be here! nil >> Blog.first.entries.active.print active active nil >> Blog.first.entries.inactive.print inactive nilNotice that the problem only occurs when an instance is stored in a variable. Going through the model using Blog.first yields the expected results. I am not familiar with how ActiveRecord handles association extensions internally, but it appears like some kind of caching is to blame for this.
-
Mani Tadayon
I created a github repo with a test app in case anyone wants to see if they can recreate this:
http://github.com/bowsersenior/test_association_extensionsAlso, here is one more example that shows how c.entries is totally fouled up after calling c.entries.inactive.print:
>> c = Blog.first #<Blog:0x237af58> { :id => 1, :created_at => Thu, 19 Aug 2010 19:36:44 UTC +00:00, :updated_at => Thu, 19 Aug 2010 19:36:44 UTC +00:00 } >> c.entries.inactive [ [0] #<Entry:0x2374a54> { :id => 3, :status => "inactive", :blog_id => 1, :created_at => Thu, 19 Aug 2010 19:37:06 UTC +00:00, :updated_at => Thu, 19 Aug 2010 19:37:06 UTC +00:00 } ] >> c.entries.inactive.print inactive nil >> c.entries.print inactive nil >> c.entries [ [0] #<Entry:0x236ccdc> { :id => 3, :status => "inactive", :blog_id => 1, :created_at => Thu, 19 Aug 2010 19:37:06 UTC +00:00, :updated_at => Thu, 19 Aug 2010 19:37:06 UTC +00:00 } ] >> Blog.first.entries [ [0] #<Entry:0x2363f10> { :id => 1, :status => "active", :blog_id => 1, :created_at => Thu, 19 Aug 2010 19:37:02 UTC +00:00, :updated_at => Thu, 19 Aug 2010 19:37:02 UTC +00:00 }, [1] #<Entry:0x2363ec0> { :id => 2, :status => "active", :blog_id => 1, :created_at => Thu, 19 Aug 2010 19:37:04 UTC +00:00, :updated_at => Thu, 19 Aug 2010 19:37:04 UTC +00:00 }, [2] #<Entry:0x2363e70> { :id => 3, :status => "inactive", :blog_id => 1, :created_at => Thu, 19 Aug 2010 19:37:06 UTC +00:00, :updated_at => Thu, 19 Aug 2010 19:37:06 UTC +00:00 } ] -
Harm Aarts
- Assigned user set to Frederick Cheung
How is this "resolved"? IMHO this is still broken. Using Rails 2.3.8 and Ruby 1.9.1