Nick Kallen颇受欢迎的has_finder插件以named_scope的方式集成到了Rails 2.x版本,例子:
class User < ActiveRecord::Base
named_scope :active, :conditions => {:active => true}
named_scope :inactive, :conditions => {:active => false}
named_scope :recent, lambda { { :conditions => ['created_at > ?', 1.week.ago] } }
end
# Standard usage
User.active # same as User.find(:all, :conditions => {:active => true})
User.inactive # same as User.find(:all, :conditions => {:active => false})
User.recent # same as User.find(:all, :conditions => ['created_at > ?', 1.week.ago])
# They're nest-able too!
User.active.recent
# same as:
# User.with_scope(:conditions => {:active => true}) do
# User.find(:all, :conditions => ['created_at > ?', 1.week.ago])
# end
has_finder中的好处同样可以在named_scope中找到 --- 你还能得到一些额外的好处。 User.all 默认作为 User.find(:all)的别名,可以之间使用。
高级
对某些需求,不要忘了has_finder的某些功能:
传递参数
给你命名的scope传递参数,便于在运行时指定条件。
class User < ActiveRecord::Base
named_scope :registered, lambda { |time_ago| { :conditions => ['created_at > ?', time_ago] }
end
User.registered 7.days.ago # same as User.find(:all, :conditions => ['created_at > ?', 7.days.ago])
Named Scope Extensions
扩展命名的scope(和association_extensions有点类似)。
class User < ActiveRecord::Base
named_scope :inactive, :conditions => {:active => false} do
def activate
each { |i| i.update_attribute(:active, true) }
end
end
end
# Re-activate all inactive users
User.inactive.activate
匿名 scope
你还可以使用默认提供的scoped来构造。(词句不好翻译)
# Store named scopes
active = User.scoped(:conditions => {:active => true})
recent = User.scoped(:conditions => ['created_at > ?', 7.days.ago])
# Which can be combined
recent_active = recent.active
# And operated upon
recent_active.each { |u| ... }
(译者注:这和javascript中的匿名函数相似:var foo = function(x) { alert(x); };)
named_scope是很好的功能。如果你还没有开始用它,现在就开始。你将来会离不开它。还是要感谢一下Nick。
原文地址:http://ryandaigle.com/articles/2008/3/24/what-s-new-in-edge-rails-has-finder-functionality
如何把named_scope和paginate结合起来?
class Product < ActiveRecord::Base
named_scope :online, :conditions => {:status => 1}, :include => [:variants, :catalogue_images, :categories]
named_scope :from_category_ids, lambda { |cat_ids| {:conditions => "categories_products.category_id IN (#{cat_ids})", :include => :categories }}
end
class CatalogueController < RaidBase
def category
@products = Product.from_category_ids(@category.leaf_ids).online.paginate :page => params[:page], :per_page => params[:per_page]
end
end
参考网址:http://pastie.org/207771
分享到:
相关推荐
will_paginate-bootstrap, 将 Twitter Bootstrap 分页组件与will_paginate集成 will_paginate-bootstrap不再维护我不再使用 Bootstrap 来使用 Rails,所以不幸的是,我不再接受请求请求或者维护这个库。 为了发布你...
rails 分页插件will_paginate 2.2.2
will_paginate分页插件 博文链接:https://foxgst.iteye.com/blog/168489
rails 分页插件will_paginate 2.1.0
rails 分页插件will_paginate
NULL 博文链接:https://liuyi-act-buaa-edu-cn.iteye.com/blog/506503
新版本的will_paginate已经抛弃了这样的做法,转而使用gem的方式,本文通过建立一个名为foobar的应用来了解一下will_paginate的用法。 ============================== C:\>rails foobar -d mysql C:\>cd foobar C:\...
因为需要安装svn比较麻烦,就直接手工下载了
jquery.paginate.js
paginate(每页数量,是否简洁分页,分页参数) 使用方式 $list = db('user')->paginate(10); 自定义参数传参 $list = db('user')->paginate(10,false,['query'=>array('id' => $id)]); 其它参数 // 总数据 $this->...
will_paginate 带无限滚动示例应用程序 使用gem 'will_paginate', '~> 3.0.7'实现无限/无限滚动的工作 Rails CRUD 应用程序示例。 gem 'faker'被用来为项目gem 'faker'虚假数据。 演示 现场演示: : 在本地开发中...
jquery.paginate实现的ajax分页
will_paginate will_paginate是一个分页库,与Ruby on Rails,Sinatra,Hanami :: View,Merb,DataMapper和Sequel集成。 安装:##用于Rails 3 +,Sinatra和Merb的Gemfile gem'will_paginate','〜> will_paginate ...
python库。 资源全名:flask-paginate-0.4.4.zip
jQuery paginate分页导航插件ajax分页代码 jQuery paginate分页导航插件ajax分页代码 jQuery paginate分页导航插件ajax分页代码
资源分类:Python库 所属语言:Python 资源全名:mkdocs_paginate_plugin-0.0.3-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
用法 <%= will_paginate @clients, renderer: WillPaginate::ActionView::BootstrapLinkRenderer %> 以下选项可用(除了will_paginate中可用的选项之外): :list_classes = ['pagination'] # Array of ...