This project is archived and is in readonly mode.
find with :include ignores default_scope
Reported by rubymood | July 21st, 2009 @ 10:03 AM | in 3.x
class Game < AR::B
has_many :articles end
class Article < AR::B
belongs_to :game default_scope :order => 'created_at DESC'
end
Calling Game.first(:include => :articles) generate this SQL
games LIMIT 1
SELECT * FROM
SELECT articles
.* FROM articles
WHERE
(articles
.game_id = 1)
When it should be
games LIMIT 1
SELECT * FROM
SELECT articles
.* FROM articles
WHERE
(articles
.game_id = 1) ORDER BY created_at
DESC
Comments and changes to this ticket
-
Jeff November 12th, 2009 @ 12:10 AM
Oh yeah, this is something I encountered when I was developing is_paranoid. preload_associations calls with_exclusive_scope which murders the default_scope on :includes
It seems default_scope's intention and (er...) scope could stand to be more explicit. A decent number of people certainly expect it to be more default than it seems to be.
-
Evan February 11th, 2010 @ 10:47 PM
I can confirm in Rails 2.3.5
And agree the behavior is unexpected.
class Book
default_scope :conditions => "visible = true" endclass Author
has_many :books endI would expect the following two cases to return the same books:
applies the default scope when not eager loading (will only have visible books)
author = Author.first
author.booksdoes not apply the default scope when eager loading (will have both visible and non visible books)
author = Author.first(:includes => :books)
author.books -
Lawrence Curtis June 20th, 2010 @ 04:57 PM
- Tag changed from 2.x, :include, active_record, default_scope to arel rails3, 2.x, 3, :include, active_record, default_scope
This is still an issue in rails3, it's REALLY annoying, as i have no way of specifying options to included objects
+1
-
Lawrence Curtis June 20th, 2010 @ 05:13 PM
if somebody could point me in the right direction im more than happy to write a patch, might need a little help with the tests etc
-
Subba July 16th, 2010 @ 06:49 PM
- Importance changed from to
Hi i found the fix for this problem.
eager loaded includes final call this method.find_associated_records in association_preload.rb
this method calls
reflection.klass.unscoped.apply_finder_options(find_options).to_a
it should use scoped which uses default_scope (unscope ignores default scope).
i am attaching patch with tests for rails 3.
-
Subba July 16th, 2010 @ 06:49 PM
- Assigned user set to Neeraj Singh
-
Neeraj Singh July 20th, 2010 @ 03:50 AM
- Assigned user changed from Neeraj Singh to José Valim
- State changed from new to open
- Importance changed from to Low
looks good
+1
-
Repository July 21st, 2010 @ 02:08 PM
- State changed from open to resolved
(from [d77c3b669ce052234868b3d8e4f066e1baf9dbd5]) eagerly loaded association records should respect default_scope [#2931 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/d77c3b669ce052234868b3d8e4f066...
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
- 2931 find with :include ignores default_scope (from [d77c3b669ce052234868b3d8e4f066e1baf9dbd5]) eagerly...