所以我有一个类似于枚举值的模型:
class Connection < ActiveRecord::Base
enum request_status: { pending: 0, accepted: 1, rejected: 2, removed: 3 }
end
但是,我有一个params值,我想设置并执行查询 - 这样的事情:
@connections = current_user.all_connections.where(request_status: params[:request_status])
current_user.all_connections
返回一个AR对象(也就是......它也是一个带有where调用的查询方法)。
我面临的问题是,现在,上面的查询返回一个空集合。我相信的原因是API docs in Rails:
enum属性的条件必须使用a的序数值 枚举。
鉴于params[:request_status]
将如下所示:
Parameters: {"request_status"=>"accepted"}
如何修改where
查询以反映这一点?我知道另一个选择只是将参数修改为序数值,而不是字符串,但我很好奇我如何用字符串值而不是序数值来实现这个?
修改1
以下是服务器日志:
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)
修改2
这是该控制器的索引操作。
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
奇怪的是,对于我点击的每个操作都应该发送正确的request_status
,它仍会使用request_status = 0
发送SQL。
我注释掉了控制器中的params[:request_status] =
setter,看看是不是这样做了,但事实并非如此。
可能导致此问题的原因是什么?
以下是另一个不同状态的日志:
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)
答案 0 :(得分:1)
您可以使用Connection.request_statuses["pending"]
获取0。
因此,您可以在查询中使用字符串,如下所示:
@connections = current_user.all_connections.where(request_status: Connection.request_statuses[params[:request_status]])