This project is archived and is in readonly mode.
named_scope not preserving proper model in STI
Reported by Paweł Kondzior | December 14th, 2008 @ 03:26 AM | in 2.3.9
Simple example:
class User < ActiveRecord::Base
named_scope(:simple_test, lambda { puts self.inspect; {} })
end
class Employee < ActiveRecord::Base
end
And the result:
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) => []
In such case we don't have access to Emloyee class methods, reflection and other from lambda.
Comments and changes to this ticket
-
Paweł Kondzior December 14th, 2008 @ 03:27 AM
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 December 14th, 2008 @ 03:33 AM
class Employee < User end
And because it's bit late, i did typo in example code :(
-
Frederick Cheung December 14th, 2008 @ 10:31 AM
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 December 14th, 2008 @ 11:29 AM
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 December 14th, 2008 @ 01:15 PM
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 December 14th, 2008 @ 03:16 PM
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 December 14th, 2008 @ 05:33 PM
Same stuff but here i'm using instance_exec functionality.
-
Pratik March 8th, 2009 @ 12:57 PM
- 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 June 23rd, 2010 @ 02:37 AM
- 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 which is similar to this one.
-
Repository June 23rd, 2010 @ 08:17 AM
(from [687d7f52c4a1d85666f1efc08ffa182e6dfeba57]) STI should identify itself inside named_scope
[#1570 state:resovled]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/687d7f52c4a1d85666f1efc08ffa18... -
José Valim June 23rd, 2010 @ 08:18 AM
- Milestone changed from 3.x to 2.3.9
- State changed from incomplete to resolved
-
James Le Cuirot July 4th, 2010 @ 02:08 PM
- Importance changed from to
This commit causes a massive slow down and memory leak. Please see #5044.
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
Tags
Referenced by
- 1570 named_scope not preserving proper model in STI [#1570 state:resovled]
- 5044 Recent named_scope STI fix causes a massive slow down and memory leak Commit 687d7f52c4a1d85666f1efc08ffa182e6dfeba57 (against...