Rails 查询列表数据筛选条件写法

1、介绍

  • 当我们要筛选不同数据的时候,会出现一种情况大量的ifunlessswitch代码做判断条件。
  • 所以我们可以使用Rails ApplicationRecord类的scope,他可以把常用的对象封装在一个方法里面,这里借用一段Rails guide的代码
class Order < ApplicationRecord
  scope :created_before, ->(time) { where("created_at < ?", time) if time.present? }
end

解释:上面代码不管是true还是false,最终结果都是返回ActiveRecord::Relation对象,where也是ActiveRecord::Relation对象。

注意if time.present?如果是false,那么他返回的是nil,因此它也会返回ActiveRecord::Relation对象,但是如果调用的方法不是ActiveRecord::Relation对象,它就会引发 NoMethodError 异常。

2、优雅的使用scope

  • 经过上面的介绍我们现在来优雅的使用它
    例子
# controller
class ArticlesController < ApplicationController
  def index
    # 即使某个参数为空,语句也能正常执行
    @articles = Article.name_condition(params[:name])
                       .phone_condition(params[:photo])
  end
end

# model 
class Article < ApplicationRecord
    scope :name_condition, ->(name) do 
        where('name = ?', name) if name.present?
    end
    
    scope :phone_condition, ->(phone) do
        where("lower(phone) like ?", "%#{phone}%") if phone.present?
    end
end

解释:上面代码就算参数是空,语句也会正常执行啦,以后所有参数判断都在scope,也可以配合gem grapeparams参数验证,这样可以省去你判断参数是否合法的问题。

提醒:其实scope在很多框架都有提供,像php laravelgolang gorm也可以使用上面这种方法。

golang gorm的使用方法:

// 编写 scope
func LocationLatitude(latitude string) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if latitude != "" {
			return db.Where("latitude = ?", latitude)
		}
		return db
	}
}
// 使用 scope
func (location Location) FindByCriteria(l Location) (respLocations []Location, err error) {
	result := db.DB.Scopes(LocationName(l.Name),
		LocationStatus(l.Status),
		LocationType(l.LocationType),
		LocationLatitude(l.Latitude),
		LocationLongitude(l.Longitude)).Find(&respLocations)
	return respLocations, result.Error
}

结束语:抱歉啊! 作者很久没做php开发了,laravel的使用代码我目前没有嘿嘿,本篇分享结束啦。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值