`

使用scope 链接多个where条件

阅读更多

  scope :by_category, (lambda do |category_id|
    where(category_id: category_id) unless category_id.nil?
  end)
  scope :by_district, (lambda do |district_id|
    where(district_id: district_id) unless district_id.nil?
  end)
#if district 是nil就会返回全部
Client.by_category(category).by_district(district)




class User
  scope :by_age, lambda do |age|
    joins(:profile).where('profile.age = ?', age) unless age.nil?
  end
  scope :by_name, lambda{ |name| where(name: name) unless name.nil? }
  scope :by_email, lambda do |email|
    joins(:profile).where('profile.email = ?', email) unless email.nil?
  end
end




wheres = [:id, :email].map{|key| params.has_key?(key) ? {key => params[key]} : {} }\
                      .inject({}){|hash, injected| hash.merge!(injected)}
@users = User.where(wheres).limit(10)





#user.rb
scope :by_status, lambda { |status| where(:status => status) unless status.blank? }

# If you need to execute a block of code you can use the following syntax
scope :by_status, (lambda do |status|
where(:active => status) unless status.blank?
end)


#Active users
pry(main)> User.by_status(1)
SELECT `users`.* FROM `users` WHERE `users`.`active` = 1

#Inactive users
pry(main)> User.by_status(0)
SELECT `users`.* FROM `users` WHERE `users`.`active` = 0

#All users
pry(main)> User.by_status(nil)
SELECT `users`.* FROM `users`

分享到:
评论

相关推荐

    SqlScope数据层

    使用using来对连接,查询,和事务进行管理,代码简洁明了。 支持多种常用的数据库访问的Method,使数据库操作代码更短。 通过把ISqlScope放到较大的范围内,可以实现该范围内数据连接的高效利用。 { 例如: ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    16.1.4. 返回多个实体(Returning multiple entities) 16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 ...

    Hibernate+中文文档

    16.1.4. 返回多个实体(Returning multiple entities) 16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 ...

    HibernateAPI中文版.chm

    16.1.4. 返回多个实体(Returning multiple entities) 16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 ...

    hibernate3.2中文文档(chm格式)

    16.1.4. 返回多个实体(Returning multiple entities) 16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 ...

    Hibernate中文详细学习文档

    16.1.4. 返回多个实体(Returning multiple entities) 16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 ...

    Hibernate参考文档

    16.1.4. 返回多个实体(Returning multiple entities) 16.1.4.1. 别名和属性引用(Alias and property references) 16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance...

    Hibernate 中文 html 帮助文档

    16.1.4. 返回多个实体(Returning multiple entities) 16.1.4.1. 别名和属性引用(Alias and property references) 16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance...

    hibernate 体系结构与配置 参考文档(html)

    混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射...

    Sosoo 1.0网络爬虫程序.doc

    它是实现了一个数组存储,你可以根据自己的需求加入多个实现DocumentFilter接口的对象,系统提供了一个实现LinkLocalizer,用来替换其中的相对连接。 系统先进行filter处理,然后在进行HttpDocManager 具体编程...

    精通windows server 2008 命令行与powershell电子书PDF版(第一卷)

    由于电子书超过80M故只能分卷压缩成4个文件,将四个压缩包下载到同一目录下解压即可得到PDF版电子书 第一卷地址: http://download.csdn.net/source/3488105 第二卷地址: ... 第三卷地址: ...Windows Server 2008系统...

    精通windows server 2008 命令行与powershell 电子书PDF版(第四卷)

    由于电子书超过80M故只能分卷压缩成4个文件,将四个压缩包下载到同一目录下解压即可得到PDF版电子书 第一卷地址: http://download.csdn.net/source/3488105 第二卷地址: ... 第三卷地址: ...精通windows server 2008 ...

    精通windows server 2008 命令行与powershell电子书PDF版(第三卷)

    由于电子书超过80M故只能分卷压缩成4个文件,将四个压缩包下载到同一目录下解压即可得到PDF版电子书 第一卷地址: http://download.csdn.net/source/3488105 第二卷地址: ... 第三卷地址: ...精通windows server 2008 ...

    精通windows server 2008 命令行与powershell电子书PDF版(第二卷)

    由于电子书超过80M故只能分卷压缩成4个文件,将四个压缩包下载到同一目录下解压即可得到PDF版电子书 第一卷地址: http://download.csdn.net/source/3488105 第二卷地址: ... 第三卷地址: ...精通windows server 2008 ...

    精通windows server 2008 命令行与powershell 电子书PDF单文件完整版

    精通windows server 2008 命令行与powershell 电子书PDF...10.4.3 &——同一行中使用多个不同命令 472 10.4.4 &&——如果多个命令中的一个失败即中止后续命令 473 10.4.5 ]sysy[ ]sysy[——允许在字符串中包含空格 473...

    外文翻译 stus MVC

    The Controller receives the request from the browser, and makes the decision where to send the request. With Struts, the Controller is a command design pattern implemented as a servlet. The struts-...

    Hibernate注释大全收藏

    以上所有实体使用 JOINED 策略 Ferry和Boat class使用同名的主键关联(eg: Boat.id = Ferry.id), AmericaCupClass 和 Boat 关联的条件为 Boat.id = AmericaCupClass.BOAT_ID. 从父类继承的属性 @MappedSuperclass ...

    2009 达内Unix学习笔记

    ln 创建硬链接或软链接,硬链接=同一文件的多个名字;软链接=快捷方式 用法 ln [-f] [-n] [-s] f1 [f2] ln [-f] [-n] [-s] f1 ... fn d1 ln [-f] [-n] -s d1 d2 ln file1 file1.ln 创建硬链接。感觉是同一文件...

Global site tag (gtag.js) - Google Analytics