This project is archived and is in readonly mode.
find with :include ignores default_scope
-
Deleted User
I can confirm this in Rails 2.3.3
-
John
I can confirm this as of 2.3.4.
-
Jeff
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
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
- 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
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
- 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_ait should use scoped which uses default_scope (unscope ignores default scope).
i am attaching patch with tests for rails 3.
-
Subba
- Assigned user set to Neeraj Singh
-
Subba
updating patch after rebasing with recent changes
-
Neeraj Singh
- Assigned user changed from Neeraj Singh to José Valim
- State changed from new to open
- Importance changed from to Low
looks good
+1
-
Subba
rubymod can you verify this patch
-
Repository
- State changed from open to resolved
(from [d77c3b669ce052234868b3d8e4f066e1baf9dbd5]) eagerly loaded association records should respect default_scope [#2931 find with :include ignores default_scope state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/d77c3b669ce052234868b3d8e4f066...
