我有一个模型 - Activity
,看起来像这样:
# == Schema Information
#
# Table name: activities
#
# id :integer not null, primary key
# trackable_id :integer
# trackable_type :string(255)
# owner_id :integer
# owner_type :string(255)
# key :string(255)
# parameters :text
# recipient_id :integer
# recipient_type :string(255)
# created_at :datetime
# updated_at :datetime
# read_status :integer default(0)
#
class Activity < PublicActivity::Activity
enum read_status: [ :unread, :read ]
attr_accessible :read_status
end
在我ActivitiesController.rb
我添加了这个:
private
# Never trust parameters from the scary internet, only allow the white list through.
def activity_params
params.require(:activity).permit(:read_status)
end
但是当我在我的控制台中执行此操作时,我会返回相同数量的记录:
[13] pry(main)> Activity.where(read_status: "read").count
(0.5ms) SELECT COUNT(*) FROM "activities" WHERE "activities"."read_status" = 0
=> 12
[14] pry(main)> Activity.where(read_status: "unread").count
(0.4ms) SELECT COUNT(*) FROM "activities" WHERE "activities"."read_status" = 0
=> 12
请注意,两个where
调用都在检查"read_status" = 0
,即使情况显然并非如此。
在这种情况下,第一个应检查read_status = 1
。
这是我第二次遇到enum这个问题。