我正在使用ActiveModel使我的无表格模型感觉像ActiveRecord模型。我正在使用自定义商店,我需要能够验证记录的唯一性。
如何使用ActiveModel实现所需的唯一性检查?
答案 0 :(得分:1)
我最终做了这样的事情:
class Theme < Base
include ActiveModel::Validations
attr_accessor :id, :name
validate :uniqueness_of_theme_name, :on => :create
def initialize(attrs = {})
attrs.each do |name, value|
send("#{name}=", value)
end
end
def uniqueness_of_theme_name
errors.add(:name, "name is already in use") unless Theme.find_by_name(name).nil?
end
def self.find_by_name(name)
return store.find({ 'name' => "/^#{name}$/i" }).any?
end
def self.create(attributes = {})
theme = Theme.new(attributes)
theme.save
return theme
end
def persisted?
id && !id.nil?
end
def save()
# only call validation during create context
context = persisted? :update : :create
false if invalid?(context)
store.save(self)
true
end
def self.create(attrs = {})
theme = Theme.new(attrs)
theme.save
theme
end
end
也许有人可以告诉我这是否是正确的方法
答案 1 :(得分:0)
检查此railscast,它可能会有所帮助
http://railscasts.com/episodes/193-tableless-model
完成模型后,添加验证,然后添加@ model.valid?
答案 2 :(得分:0)
ActiveModel可以根据另一个railscasts使用,这更适合该用例。
class Theme
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :id, :name
validates :name, uniqueness: true, on: :create
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end