This project is archived and is in readonly mode.
named_scope not preserving proper model in STI
-
Paweł Kondzior
Loading development environment (Rails 2.3.0) >> Employee.simple_test(123) User(id: integer, created_at: datetime, updated_at: datetime, active: boolean, type: string, location_id: integer, name: string, email: string) => [] -
Paweł Kondzior
class Employee < User endAnd because it's bit late, i did typo in example code :(
-
Frederick Cheung
This is not so much named_scope doing something but ruby closures. The block associated with the lambda remembers what self was when that block was made.
-
Paweł Kondzior
Yup, i figured out that. But still, problem exists. Workaround could be redefine simple_test from Employee method, but we do STI to be DRY, this is not DRY. The best solution would be Proc#call_with_binding in Ruby (hard thing!) or rework of scopes, and create some helper method for all lambdas.
For Proc#call_with_binding... well it's funny but we have some similar proposals for 1.9? http://www.ruby-forum.com/topic/...
The second solution would be probably very ugly and no one from core will acceppt such and api for lambdas ? Am i right ?
-
Frederick Cheung
Or if the @proxy_scope variable from the Scope (which should be the "right" class/scope) were somehow passed to the lambda, but I'm not sure how you'd do that without breaking everyone's existing code.
-
Paweł Kondzior
I hoped that solution will be easy, and i was right :) Active Support gives us Proc#bind that emulates 1.9 instance_exec (instance_eval with arguments)
It was after i post proposal http://www.ruby-forum.com/topic/..., details about instance_exec you can found here http://eigenclass.org/hiki.rb?in...
I thnik DHH and rest of core should lobby ruby-core for Proc#bind native implementation in 1.9, it's better to have it in ruby than in active_support just like Symbol#to_proc. Just use your power that comes from being core of ruby community ;-)
Small patch with testcase is on the way.
-
Paweł Kondzior
And here it is.
-
Paweł Kondzior
Same stuff but here i'm using instance_exec functionality.
-
Pratik
- Assigned user changed from Jeremy Kemper to Pratik
- Tag changed from 2.3, bug, named_scope, sti to 2.3, bug, named_scope, patch, sti
- State changed from new to incomplete
Patch doesn't apply any more. Also, we should have a test for cases when such a named scope is chained with other named scopes.
Thanks.
-
Neeraj Singh
- Assigned user changed from Pratik to José Valim
Attached is test.
Attached is code patch. For the code patch please provide credit to Paweł Kondzior. One of these days I will figure out myself how to do that.
Assigning this ticket to Mr. Valim . He approved #4507 STI ignored with scopes which is similar to this one.
-
Neeraj Singh
Here is test.
-
Repository
(from [687d7f52c4a1d85666f1efc08ffa182e6dfeba57]) STI should identify itself inside named_scope
[#1570 named_scope not preserving proper model in STI state:resovled]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/687d7f52c4a1d85666f1efc08ffa18... -
José Valim
- Milestone changed from 3.x to 2.3.9
- State changed from incomplete to resolved
-
James Le Cuirot
- Importance changed from to
This commit causes a massive slow down and memory leak. Please see #5044 Recent named_scope STI fix causes a massive slow down and memory leak.
