According to the post, we can create a function in model.
class Item < ActiveRecord::Base
enum status: [:unknown,:Pending, :Active, :Waiting,:Delete,:Reject,:Finish]
scope :status, -> (status) { where status: status }
end
While status is enum, i tried several way to make it work
in Controller,
@items.status(1) # (o)
@items.status(:Pending) #(x) without compiling error
@items.status("Pending") #(x) without compiling error
@items.status(Item.statuses[:Pending]) # (o)
Make sure it’s statuses[:Pending] not status
None of them contribute runtime error, but it’s always false. It seemed strange…
in View, use string to compare
<% if item.status == "Pending"%> # (o)
<% if item.status == 1%> # (x)
<% if item.status == :Pending%> # (x)
<% if item.status == Item.statuses[:Pending]%> # (x)
<% if item.Pending? %> # (o)
<% if item.status.Pending? %> # (x) runtime error
My speculation is that it converts property into string for rendering.