[英]How do I perform a where query on an enum attribute with a string rather than the ordinal value?
So I have a model that has an enum value like so: 所以我有一个具有枚举值的模型,如下所示:
class Connection < ActiveRecord::Base
enum request_status: { pending: 0, accepted: 1, rejected: 2, removed: 3 }
end
But, I have a params value that I want to set and do a where query on - something like this: 但是,我有一个要设置的params值,并对其执行where查询-像这样:
@connections = current_user.all_connections.where(request_status: params[:request_status])
current_user.all_connections
returns an AR object (aka...it is also a query method with a where call). current_user.all_connections
返回一个AR对象(又名...这也是一个带where调用的查询方法)。
The issue I am facing is that right now, the above query returns an empty collection. 我面临的问题是,现在,以上查询返回一个空集合。 The reason I believe is that per the API docs in Rails :
我相信的原因是根据Rails中的API文档 :
Where conditions on an enum attribute must use the ordinal value of an enum.
枚举属性上的条件必须使用枚举的序数值的情况。
Given that params[:request_status]
will look like this: 鉴于
params[:request_status]
看起来像这样:
Parameters: {"request_status"=>"accepted"}
How would I modify that where
query to reflect this? 我将如何修改
where
查询,以反映这一点? I know one other option is just to modify the parameter to be the ordinal value, rather than the string, but I am curious how I might achieve this with the string value rather than the ordinal value? 我知道另一个选择就是将参数修改为序号值,而不是字符串,但是我很好奇如何用字符串值而不是序号值实现此目的?
Edit 1 编辑1
Here are the server logs: 这是服务器日志:
Started GET "/connections?request_status=accepted" for 127.0.0.1 at 2016-01-04 20:23:08 -0500
Processing by ConnectionsController#index as HTML
Parameters: {"request_status"=>"accepted"}
User Load (2.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1
FamilyTree Load (1.7ms) SELECT "family_trees".* FROM "family_trees" WHERE "family_trees"."user_id" = $1 LIMIT 1 [["user_id", 3]]
Connection Load (2.0ms) SELECT "connections".* FROM "connections" WHERE "connections"."invited_user_id" = $1 ORDER BY "connections"."created_at" DESC [["invited_user_id", 3]]
Connection Load (1.8ms) SELECT "connections".* FROM "connections" WHERE "connections"."inviter_user_id" = $1 ORDER BY "connections"."created_at" DESC [["inviter_user_id", 3]]
Connection Load (2.2ms) SELECT "connections".* FROM "connections" WHERE (connections.invited_user_id = 3 OR connections.inviter_user_id = 3) AND "connections"."request_status" = 0 ORDER BY "connections"."created_at" DESC
Rendered connections/index.html.erb within layouts/connections (0.3ms)
Rendered connections/_header.html.erb (2.8ms)
Rendered shared/_navbar.html.erb (61.2ms)
Rendered shared/_footer.html.erb (3.2ms)
Rendered layouts/application.html.erb (1142.4ms)
Completed 200 OK in 1184ms (Views: 1155.5ms | ActiveRecord: 10.4ms)
Edit 2 编辑2
This is the index action for that controller. 这是该控制器的索引操作。
def index
params[:request_status] = "pending" if params[:request_status].nil?
@connections = current_user.all_connections.where(request_status: params[:request_status]).order(created_at: :desc).group_by { |c| c.created_at.to_date }
end
What's strange is that for every action that I click on that should send the correct request_status
it still sends the SQL with request_status = 0
. 奇怪的是,对于我单击的每个应发送正确的
request_status
它仍会发送request_status = 0
的SQL。
I commented out the params[:request_status] =
setter in the controller to see if that was doing it, but it wasn't it. 我在控制器中注释掉了
params[:request_status] =
setter,看看是否正在这样做,但不是。
What could be causing this issue? 是什么导致此问题?
Here is another log for a different status: 这是另一种状态不同的日志:
Started GET "/connections?request_status=rejected" for 127.0.0.1 at 2016-01-04 21:30:29 -0500
Processing by ConnectionsController#index as HTML
Parameters: {"request_status"=>"rejected"}
User Load (2.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1
FamilyTree Load (1.5ms) SELECT "family_trees".* FROM "family_trees" WHERE "family_trees"."user_id" = $1 LIMIT 1 [["user_id", 3]]
Connection Load (1.4ms) SELECT "connections".* FROM "connections" WHERE "connections"."invited_user_id" = $1 ORDER BY "connections"."created_at" DESC [["invited_user_id", 3]]
Connection Load (1.0ms) SELECT "connections".* FROM "connections" WHERE "connections"."inviter_user_id" = $1 ORDER BY "connections"."created_at" DESC [["inviter_user_id", 3]]
Connection Load (1.2ms) SELECT "connections".* FROM "connections" WHERE (connections.invited_user_id = 3 OR connections.inviter_user_id = 3) AND "connections"."request_status" = 0 ORDER BY "connections"."created_at" DESC
Rendered connections/index.html.erb within layouts/connections (0.2ms)
Rendered connections/_header.html.erb (2.5ms)
Rendered shared/_navbar.html.erb (60.3ms)
Rendered shared/_footer.html.erb (3.0ms)
Rendered layouts/application.html.erb (1103.8ms)
Completed 200 OK in 1130ms (Views: 1112.5ms | ActiveRecord: 7.3ms)
You can use Connection.request_statuses["pending"]
to get 0. 您可以使用
Connection.request_statuses["pending"]
来获取0。
So you can use string in your query like this: 因此,您可以在查询中使用字符串,如下所示:
@connections = current_user.all_connections.where(request_status: Connection.request_statuses[params[:request_status]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.