Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在创建回调之前,如何验证是否存在重复记录,如果存在,如何返回?_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 在创建回调之前,如何验证是否存在重复记录,如果存在,如何返回?

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 &

在创建记录之前,我想验证是否不存在类似的记录

当我说相似时,我想比较3-4列,看看它们是否具有相同的值,如果它们具有相同的值,则不保存并报告验证消息

这只发生在创建时,在更新等过程中可能会出现重复。但在创建期间,我不希望允许重复


我该怎么做?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
更多信息存在于这里