named_scope does not respect eager loading
Reported by Dennis Krupenik | June 15th, 2008 @ 12:42 PM
class Person
has_many :messages
end
class Message
belongs_to :person
named_scope :unread, :conditions => { :is_read => false }
end
@person = Person.first(:include => :messages) # performs an sql query
@person.messages.unread # performs another sql query. should it?
@person.messages.find_all { |msg| !msg.is_read } # does not perform sql query.
@person.messages.unread.find_all { true } # completely ignores '.unread'. should it?
i'm not sure whether it is a bug or a feature.
is there a way to utilize existing named_scopes to perform filtering on already loaded associations without additional queries?
Comments and changes to this ticket
-

elDub July 17th, 2008 @ 03:24 PM
- → Tag changed from to eager_loading named_scope
I have been battling with this issue as well. I think the problem is more related to 'has_one' relationships though.
class Document < ActiveRecord::Base has_one :revision, :class_name => 'Document', :foreign_key => 'prior_revision_id' belongs_to :prior_revision, :class_name => 'Document', :foreign_key => 'prior_revision_id' named_scope :active, :conditions => { :active => true } named_scope :inactive, :conditions => { :active => false } endAnd then in script/console:
Loading development environment (Rails 2.1.0) >> d1 = Document.create(:active => false, :title => 'd1', :body => 'text') => #<Document id: 1, prior_revision_id: nil, title: "d1", body: "text", active: false, created_at: "2008-07-17 14:18:36", updated_at: "2008-07-17 14:18:36"> >> d2 = d1.create_revision(:active => true, :title => 'd2', :body => 'new text') => #<Document id: 2, prior_revision_id: 1, title: "d2", body: "new text", active: true, created_at: "2008-07-17 14:19:14", updated_at: "2008-07-17 14:19:14"> >> doc = Document.find(1, :include => :revision) => #<Document id: 1, prior_revision_id: nil, title: "d1", body: "text", active: false, created_at: "2008-07-17 14:18:36", updated_at: "2008-07-17 14:18:36"> >> doc.revision => #<Document id: 2, prior_revision_id: 1, title: "d2", body: "new text", active: true, created_at: "2008-07-17 14:19:14", updated_at: "2008-07-17 14:19:14"> >> ns_doc = Document.inactive.find(:all, :include => :revision)[0] => #<Document id: 1, prior_revision_id: nil, title: "d1", body: "text", active: false, created_at: "2008-07-17 14:18:36", updated_at: "2008-07-17 14:18:36"> >> ns_doc.revision => nilNote how the attempt to use eager loading within a named scope request causes the same document (id: 1) to NOT be able to access it's revision.
-

elDub July 17th, 2008 @ 03:37 PM
Please forgive/ignore my prior post. After re-thinking it, it is a different issue which I will create a new post for.
-
Tarmo Tänav September 18th, 2008 @ 06:51 AM
- → Tag changed from eager_loading named_scope to eager_loading named_scope
- → State changed from new to invalid
Since conditions on named_scope's can be arbitrarily complex SQL snippets it is impossible to filter the association by the named scope in AR alone, making an exception for simple hash based conditions does not seem likely to happen.
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
Repository is at http://github.com/rails/rails
Check out the development master (Edge Rails):
git clone git://github.com/rails/rails.git
Creating or reviewing a patch
See the contributor guide.
Creating a feature request
Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..
