Ruby-on-rails 如何在没有验证的情况下更新属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2998780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:45:05  来源:igfitidea点击:

How to update attributes without validation

ruby-on-railsvalidation

提问by Brian Roisentul

I've got a model with its validations, and I found out that I can't update an attribute without validating the object before.

我有一个带有验证的模型,我发现我无法在没有验证对象的情况下更新属性。

I already tried to add on => :createsyntax at the end of each validation line, but I got the same results.

我已经尝试on => :create在每个验证行的末尾添加语法,但得到了相同的结果。

My announcement model have the following validations:

我的公告模型具有以下验证:

  validates_presence_of :title
  validates_presence_of :description
  validates_presence_of :announcement_type_id

  validate :validates_publication_date
  validate :validates_start_date
  validate :validates_start_end_dates
  validate :validates_category
  validate :validates_province

  validates_length_of :title, :in => 6..255, :on => :save
  validates_length_of :subtitle, :in => 0..255, :on => :save
  validates_length_of :subtitle, :in => 0..255, :on => :save
  validates_length_of :place, :in => 0..50, :on => :save

  validates_numericality_of :vacants, :greater_than_or_equal_to => 0,  :only_integer => true
  validates_numericality_of :price, :greater_than_or_equal_to => 0,  :only_integer => true

My rake task does the following:

我的 rake 任务执行以下操作:

  task :announcements_expiration => :environment do
    announcements = Announcement.expired

    announcements.each do |a|
      #Gets the user that owns the announcement
      user = User.find(a.user_id)
      puts a.title + '...'

      a.state = 'deactivated'

      if a.update_attributes(:state => a.state)
        puts 'state changed to deactivated'
      else
        a.errors.each do |e|
          puts e
        end

      end
    end

This throws all the validation exceptions for that model, in the output.

这会在输出中抛出该模型的所有验证异常。

Does anybody how to update an attribute without validating the model?

有没有人如何在不验证模型的情况下更新属性?

回答by Olivier Grimard

You can do something like:

您可以执行以下操作:

object.attribute = value
object.save(:validate => false)

回答by Salil

USE update_attributeinstead of update_attributes

使用update_attribute而不是update_attributes

Updates a single attribute and saves the record without going through the normal validation procedure.

更新单个属性并保存记录,而无需通过正常的验证程序。

if a.update_attribute('state', a.state)

Note:- 'update_attribute' update only one attribute at a time from the code given in question i think it will work for you.

注意:- 'update_attribute' 从有问题的代码中一次只更新一个属性,我认为它对你有用。

回答by res

try using

尝试使用

@record.assign_attributes({ ... })
@record.save(validate: false)

works for me

对我来说有效

回答by William Wong Garay

Yo can use:

你可以使用:

a.update_column :state, a.state

Check: http://apidock.com/rails/ActiveRecord/Persistence/update_column

检查:http: //apidock.com/rails/ActiveRecord/Persistence/update_column

Updates a single attribute of an object, without calling save.

更新对象的单个属性,而不调用 save。

回答by MZaragoza

All the validation from model are skipped when we use validate: false

当我们使用时,将跳过模型的所有验证 validate: false

user = User.new(....)
user.save(validate: false)

回答by Saifis

Shouldn't that be

不应该是

validates_length_of :title, :in => 6..255, :on => :create

so it only works during create?

所以它只在创建期间有效?