How to update attributes without validation

Ruby on-RailsValidation

Ruby on-Rails Problem Overview


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 => :create syntax at the end of each validation line, but I got the same results.

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:

  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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You can do something like:

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

Solution 2 - Ruby on-Rails

USE update_attribute instead of 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.

Solution 3 - Ruby on-Rails

try using

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

works for me

Solution 4 - Ruby on-Rails

Yo can use:

a.update_column :state, a.state

Check: http://apidock.com/rails/ActiveRecord/Persistence/update_column > Updates a single attribute of an object, without calling save.

Solution 5 - Ruby on-Rails

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

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

Solution 6 - Ruby on-Rails

Shouldn't that be

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

so it only works during create?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian RoisentulView Question on Stackoverflow
Solution 1 - Ruby on-RailsOlivier GrimardView Answer on Stackoverflow
Solution 2 - Ruby on-RailsSalilView Answer on Stackoverflow
Solution 3 - Ruby on-RailsresView Answer on Stackoverflow
Solution 4 - Ruby on-RailsWilliam Wong GarayView Answer on Stackoverflow
Solution 5 - Ruby on-RailsMZaragozaView Answer on Stackoverflow
Solution 6 - Ruby on-RailsSaifisView Answer on Stackoverflow