简体   繁体   中英

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:

@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).

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 :

Where conditions on an enum attribute must use the ordinal value of an enum.

Given that params[:request_status] will look like this:

Parameters: {"request_status"=>"accepted"}

How would I modify that where query to reflect this? 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

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

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 .

I commented out the params[:request_status] = setter in the controller to see if that was doing it, but it wasn't it.

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.

So you can use string in your query like this:

@connections = current_user.all_connections.where(request_status: Connection.request_statuses[params[:request_status]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM