What's the significance of named scope in Rails?

后端 未结 3 1687
既然无缘
既然无缘 2021-01-15 04:09

Before going for details.

Question 1:-- What\'s the meaning of scope here (ie named **scope)?**

what\'s the benefits of using named

3条回答
  •  北海茫月
    2021-01-15 04:39

    we get shorter, chainable, and more readable code:

    orders = Orders.checks.last_n_days(7)
    

    is much more readable, shorter and not chainable than

    orders = Orders.all :conditions => ["updated < ? and pay_type='check'", 7]
    

    In Rails3 the advantage will be even greater, because of arel. For more information I recommend watching the Railscasts:

    1. 108 named_scope (some basics in rails 2)
    2. 202 Active Record Queries in Rails 3 (some basics in rails 3)
    3. 215 Advanced Queries in Rails 3 (some advanced topics in rails 3)

提交回复
热议问题