rails 5枚举,其中“喜欢” [英] rails 5 enum where "like"

查看:86
本文介绍了rails 5枚举,其中“喜欢”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查询一个activerecord模型枚举,并在where方法中使用like运算符,但这是行不通的。有什么技巧可以让我以这种方式查询枚举吗?在常规列上工作正常。

I'm trying to query an activerecord model enum and use the like operator in the where method, and it just doesnt work. Is there some trick to allow me to query an enum this way? Works fine on regular columns. Here it is in the console.

常规字符串列(标题)如下所示

Regular string column (title) works as shown below

irb(main):092:0> Proposal.select(:id,:department,:status).where('title like "test%"')
Proposal Load (0.3ms)  SELECT  "proposals"."id", "proposals"."department", "proposals"."status" FROM "proposals" WHERE (title like "test%") LIMIT ?  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Proposal id: 7, department: 1, status: "In Progress">, #<Proposal id: 61, department: 2, status: "Won">]>

但是,在枚举上尝试不会产生任何结果。

However, trying it on an enum, gives no results.

irb(main):094:0> Proposal.select(:department,:status).where('status like "Wo%"')
Proposal Load (0.3ms)  SELECT  "proposals"."department", "proposals"."status" FROM "proposals" WHERE (status like "Wo%") LIMIT ?  [["LIMIT", 11]]
=> #<ActiveRecord::Relation []>

有人知道为什么不能在枚举中使用像运算符吗?我试图用它来过滤datatables.net服务器端处理的视图。

Any idea why I can't use like operator on enum? I'm trying to use this to filter a view with datatables.net server side processing.

推荐答案

枚举将数据存储为整数,例如0,1,2,3 ...然后将数字映射到模型中定义的枚举值。这就是为什么您没有得到结果的原因

Enum stores data as integer like 0,1,2,3... Then rails map number to enum value defined in model. That is the reason why you doesn't get result

这篇关于rails 5枚举,其中“喜欢”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆