【发布时间】:2014-02-15 18:41:37
【问题描述】:
我正在处理一个 RoR 项目,我想对我的一个模型进行唯一性验证,以检查自定义范围:
class Keyword < ActiveRecord::Base
belongs_to :keyword_list
scope :active, -> { where("expiration > ?", DateTime.now) }
validates :name, uniqueness: { scope: [:active, :keyword_list_id] }
end
只是,这不起作用。它检查数据库中不存在的活动列并抛出此错误:
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column keywords.active does not exist
那么,我的问题是有什么方法可以使这项工作,还是我必须编写一个自定义验证器?如果是这样,是否有关于过度访问数据库应该是什么样子的任何提示?
【问题讨论】:
标签: ruby-on-rails validation activerecord activemodel named-scopes