ruby on rails named scope implementation

后端 未结 3 1602
小鲜肉
小鲜肉 2021-02-06 18:34

From the book Agile Web Development With Rails

class Order < ActiveRecord::Base
   named_scope :last_n_days, lambda { |days| {:conditions =>
      [\'updat         


        
相关标签:
3条回答
  • 2021-02-06 18:52

    You might want to try this

    class Order < ActiveRecord::Base
    
      class << self
        def last_n_days(n)
          scoped(:conditions => ['updated < ?', days])
        end
        def checks
          scoped(:conditions => {:pay_type => :check})
        end
      end
    
    end
    

    usage is the same

    @orders = Order.last_n_days(5)
    @orders = Order.checks
    @orders = Order.checks.last_n_days(5)
    

    This still does all the lazy loading you love. That is, it won't make a query until you attempt to access the records. Bonus: Rails 3 compatible!

    Named Scopes Are Dead

    0 讨论(0)
  • 2021-02-06 19:03

    Very cool. I was thinking of doing something like this in Javascript but Javascript behaves rather weird.

    The statement:

    var x = SomeObject;
    

    does not call SomeObject's toString() function. But the statement:

    var x;
    x = SomeObject;
    

    correctly calls the toString() function as expected.

    This prevents Javascript from doing cool stuff with chaining. =(

    0 讨论(0)
  • 2021-02-06 19:05

    There are two tricks (or patterns if you will) employed in the named_scope magic.

    Proxy pattern - calling a named scope method on a class or an association always returns an instance of the ActiveRecord::NamedScope::Scope class, not a colleciton of filtered AR objects. This pattern, altough very useful, makes things kind of blurry sometimes, since the proxy objects are ambivalent in their nature.

    Lazy loading - thanks to lazy loading (which in this context means - hitting the database only if neccessary) named scopes can be chained up to the point when you need to work with the collection defined by the scopes. Whenever you request the underlying colleciton, all the chained scopes are evaluated and a database query is executed.

    One final note: There's one thing to have in mind when playing with named scopes (or with any thing that uses delegation of some kind) in IRB. Everytime you hit Enter, the thing you wrote beforehand is evaluated and the inspect method is called on the returned value. In the case of chained named scopes, although the whole expression is evaluated to a Scope instance, when the IRB calls the inspect method on it, the scopes are evaluated and the database query is fired. This is caused by the fact that the inspect method is by means of delegation propagated through all the scope objects up to the underlying collection.

    0 讨论(0)
提交回复
热议问题