Ruby on rails 在创建回调之前,如何验证是否存在重复记录,如果存在,如何返回?
在创建记录之前,我想验证是否不存在类似的记录 当我说相似时,我想比较3-4列,看看它们是否具有相同的值,如果它们具有相同的值,则不保存并报告验证消息 这只发生在创建时,在更新等过程中可能会出现重复。但在创建期间,我不希望允许重复Ruby on rails 在创建回调之前,如何验证是否存在重复记录,如果存在,如何返回?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,在创建记录之前,我想验证是否不存在类似的记录 当我说相似时,我想比较3-4列,看看它们是否具有相同的值,如果它们具有相同的值,则不保存并报告验证消息 这只发生在创建时,在更新等过程中可能会出现重复。但在创建期间,我不希望允许重复 我该怎么做?i、 e.哪种回调最适合此操作,以及如何添加到验证错误集合?我会这样做: validate :has_similar_record, :on => :create ... private def has_similar_record if &
我该怎么做?i、 e.哪种回调最适合此操作,以及如何添加到验证错误集合?我会这样做:
validate :has_similar_record, :on => :create
...
private
def has_similar_record
if <Class>.find_by_column1_and_column2_and_column3(column1, column2, column3).present?
self.errors.add(:base, "record already exists with similar values")
end
end
validate:具有类似的记录,:on=>:创建
...
私有的
def有类似的记录
if.find_by_column1_和_column2_和_column3(column1,column2,column3)。是否存在?
self.errors.add(:base,“已存在具有类似值的记录”)
结束
结束
我会这样做:
validate :has_similar_record, :on => :create
...
private
def has_similar_record
if <Class>.find_by_column1_and_column2_and_column3(column1, column2, column3).present?
self.errors.add(:base, "record already exists with similar values")
end
end
validate:具有类似的记录,:on=>:创建
...
私有的
def有类似的记录
if.find_by_column1_和_column2_和_column3(column1,column2,column3)。是否存在?
self.errors.add(:base,“已存在具有类似值的记录”)
结束
结束
我会这样做:
validate :has_similar_record, :on => :create
...
private
def has_similar_record
if <Class>.find_by_column1_and_column2_and_column3(column1, column2, column3).present?
self.errors.add(:base, "record already exists with similar values")
end
end
validate:具有类似的记录,:on=>:创建
...
私有的
def有类似的记录
if.find_by_column1_和_column2_和_column3(column1,column2,column3)。是否存在?
self.errors.add(:base,“已存在具有类似值的记录”)
结束
结束
我会这样做:
validate :has_similar_record, :on => :create
...
private
def has_similar_record
if <Class>.find_by_column1_and_column2_and_column3(column1, column2, column3).present?
self.errors.add(:base, "record already exists with similar values")
end
end
validate:具有类似的记录,:on=>:创建
...
私有的
def有类似的记录
if.find_by_column1_和_column2_和_column3(column1,column2,column3)。是否存在?
self.errors.add(:base,“已存在具有类似值的记录”)
结束
结束
您可以使用first\u或\u create
链接多个where子句,如果不存在,则创建它
例如
Post.where(author: 'tom').where(subject: 'Rails is awesome').first_or_create!
post_exists = Post.where(author: 'tom').where(subject: 'Rails is awesome').exists?
这将引发异常
此处提供更多信息(假设您使用的是Rails 3.2+)
更新
对不起,我误解了你的问题。如果对象已存在,则不希望创建该对象。所以您可以使用exists?
方法,该方法可以在模型或关系上调用
例如
Post.where(author: 'tom').where(subject: 'Rails is awesome').first_or_create!
post_exists = Post.where(author: 'tom').where(subject: 'Rails is awesome').exists?
如果返回true,我们可以将错误添加到self
self.errors.add(:post, "already exists") if post_exists == true
这可以包装在自定义验证器方法中,如果post_exists为true,该方法将返回false
def validate_existence_of_post
post_exists = Post.where(author: 'tom').where(subject: 'Rails is awesome').exists?
if post_exists == true
self.errors.add(:post, "already exists")
return false
end
true
end
有关此处存在的更多信息您可以使用
first\u或\u create
链接多个where子句,如果不存在,则创建它
例如
Post.where(author: 'tom').where(subject: 'Rails is awesome').first_or_create!
post_exists = Post.where(author: 'tom').where(subject: 'Rails is awesome').exists?
这将引发异常
此处提供更多信息(假设您使用的是Rails 3.2+)
更新
对不起,我误解了你的问题。如果对象已存在,则不希望创建该对象。所以您可以使用exists?
方法,该方法可以在模型或关系上调用
例如
Post.where(author: 'tom').where(subject: 'Rails is awesome').first_or_create!
post_exists = Post.where(author: 'tom').where(subject: 'Rails is awesome').exists?
如果返回true,我们可以将错误添加到self
self.errors.add(:post, "already exists") if post_exists == true
这可以包装在自定义验证器方法中,如果post_exists为true,该方法将返回false
def validate_existence_of_post
post_exists = Post.where(author: 'tom').where(subject: 'Rails is awesome').exists?
if post_exists == true
self.errors.add(:post, "already exists")
return false
end
true
end
有关此处存在的更多信息您可以使用
first\u或\u create
链接多个where子句,如果不存在,则创建它
例如
Post.where(author: 'tom').where(subject: 'Rails is awesome').first_or_create!
post_exists = Post.where(author: 'tom').where(subject: 'Rails is awesome').exists?
这将引发异常
此处提供更多信息(假设您使用的是Rails 3.2+)
更新
对不起,我误解了你的问题。如果对象已存在,则不希望创建该对象。所以您可以使用exists?
方法,该方法可以在模型或关系上调用
例如
Post.where(author: 'tom').where(subject: 'Rails is awesome').first_or_create!
post_exists = Post.where(author: 'tom').where(subject: 'Rails is awesome').exists?
如果返回true,我们可以将错误添加到self
self.errors.add(:post, "already exists") if post_exists == true
这可以包装在自定义验证器方法中,如果post_exists为true,该方法将返回false
def validate_existence_of_post
post_exists = Post.where(author: 'tom').where(subject: 'Rails is awesome').exists?
if post_exists == true
self.errors.add(:post, "already exists")
return false
end
true
end
有关此处存在的更多信息您可以使用
first\u或\u create
链接多个where子句,如果不存在,则创建它
例如
Post.where(author: 'tom').where(subject: 'Rails is awesome').first_or_create!
post_exists = Post.where(author: 'tom').where(subject: 'Rails is awesome').exists?
这将引发异常
此处提供更多信息(假设您使用的是Rails 3.2+)
更新
对不起,我误解了你的问题。如果对象已存在,则不希望创建该对象。所以您可以使用exists?
方法,该方法可以在模型或关系上调用
例如
Post.where(author: 'tom').where(subject: 'Rails is awesome').first_or_create!
post_exists = Post.where(author: 'tom').where(subject: 'Rails is awesome').exists?
如果返回true,我们可以将错误添加到self
self.errors.add(:post, "already exists") if post_exists == true
这可以包装在自定义验证器方法中,如果post_exists为true,该方法将返回false
def validate_existence_of_post
post_exists = Post.where(author: 'tom').where(subject: 'Rails is awesome').exists?
if post_exists == true
self.errors.add(:post, "already exists")
return false
end
true
end
更多信息存在于这里