Using Update_Columns in Rails 3

How to update multiple columns in Ruby on Rails 3?

You can use update_all as follows:

User.where(:id => user.id).update_all({:field1 => true, :field2 => true})

This will generate the following update statement (mysql):

UPDATE users SET field1 = 1, field2 = 1 WHERE users.id = <whatever>

Callbacks and validations will not be run.

update_column method for multiple attributes

You can use update_columns(attr1: val1, attr2: val2, attr3: val3) as documented here. You just need to pass in the key-value pairs, not an actual hash object.

Rails: Update model attribute without invoking callbacks

Rails 3.1 introduced update_column, which is the same as update_attribute, but without triggering validations or callbacks:

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

update_columns update a string but it's nil when fetching on rails

You have some method defined on Document class (or included module) that is overriding the default attribute accessor.

To find out which, write this in console:

Document.find(14).method(:path).source_location

In any case you can access directly the attribute with

Document.find(14)['path']

Rails 3: Database column won't update through active record

remove :role from this line attr_accessor :password, :password_confirmation, :role



Related Topics



Leave a reply



Submit