Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

named_scope not preserving proper model in STI

#1570

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.

Reported by Paweł Kondzior · December 14th, 2008 @ 03:26 AM

State: resolved
Milestone: 2.3.9
Assigned to: José Valim José Valim
Importance: none

Activity

  1. Paweł Kondzior
    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)
    => []
    

    December 14th, 2008 @ 03:27 AM

  2. Paweł Kondzior
    Paweł Kondzior
    
    class Employee < User
    end
    

    And because it's bit late, i did typo in example code :(

    December 14th, 2008 @ 03:33 AM

  3. Frederick Cheung
    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.

    December 14th, 2008 @ 10:31 AM

  4. Paweł Kondzior
    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 ?

    December 14th, 2008 @ 11:29 AM

  5. Frederick Cheung
    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.

    December 14th, 2008 @ 01:15 PM

  6. Paweł Kondzior
    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.

    December 14th, 2008 @ 03:16 PM

  7. Paweł Kondzior
  8. Paweł Kondzior
  9. Pratik
    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.

    March 8th, 2009 @ 12:57 PM

  10. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  11. Neeraj Singh
    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.

    June 23rd, 2010 @ 02:37 AM

  12. Neeraj Singh
  13. Repository
  14. José Valim
    José Valim
    • Milestone changed from 3.x to 2.3.9
    • State changed from incomplete to resolved

    June 23rd, 2010 @ 08:18 AM

  15. James Le Cuirot
    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.

    July 4th, 2010 @ 02:08 PM