gpt4 book ai didi

ruby-on-rails - 用户列表中的唯一性 activerecord 验证

转载 作者:数据小太阳 更新时间:2023-10-29 08:35:25 24 4
gpt4 key购买 nike

我有一个用户模型和一个请求模型,

class Request < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
validates :status, inclusion: { in: %w(PENDING
RUNNING
COMPLETED
FAILED
ERROR) }

validates :status, on: :create,
uniqueness: { scope: :user_id,
message: "There is another request for this user",
conditions: -> { where(status: ['PENDING',
'RUNNING',
'ERROR']) }

}

end

唯一性验证背后的想法是,如果该用户有另一个 PENDING、RUNNING 或 ERROR 请求,则您无法为该用户创建请求。

it 'cannot a new pending request if user allredy has another RUNNING task' do
described_class.create!(user: user, status: 'RUNNING')

expect { described_class.create!(user: user) }.to raise_error(ActiveRecord::RecordInvalid,
/There is another request for this user/)
end

此测试失败,因为即使创建了另一个正在运行的请求,它也可以创建另一个请求。

我想那是因为我在条件上做错了。

这是创建请求模型的迁移。

class CreateRequests < ActiveRecord::Migration
def up
create_table :requests do |t|
t.integer :reason
t.string :status, default: 'PENDING', :limit => 15
t.integer :user_id

t.timestamps null: false
end
end

def down
drop_table :requests
end
end

最佳答案

您的问题是您尝试使用 validates ... uniqueness: 内置方法做太多事情。该方法假定列值必须唯一,而不是在集合中唯一

(您尝试在 conditions 参数中定义它似乎是合理的,但这实际上只是限制了被比较的记录的范围against;它不会改变“唯一列”表示“列表中唯一”。)

我建议改为将此验证编写为自定义方法 - 例如,类似于:

validate :unique_status_within_collection, on: :create

def unique_status_within_collection
unique_statuses = ['PENDING', 'RUNNING', 'ERROR']
if unique_statuses.include?(status) && Request.exists?(user: user, status: unique_statuses)
errors.add(:status, "There is another request for this user")
end
end

关于ruby-on-rails - 用户列表中的唯一性 activerecord 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49855052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com