Rails: update an attribute to nil

rails update attribute
rails params empty string to nil
rails nullify blank attributes
rails not nil
rails assign attribute
rails update_attribute
rails update vs update_attributes
activerecord default value

I have an integer column post_id that has a value in the db and I would like to update it to nil from the controller.

I have tried the following but unfortunately the value remains the same and does not change to nil:

current_user.account.update(post_id: nil)
current_user.account.update(post_id: 0)

Any ideas on how I can update the post id to nil?

Update 1

Account table

create_table "accounts", force: :cascade do |t|
  t.string "name"
  t.string "street"
  t.string "city"
  t.string "state"
  t.string "postal_code"
  t.string "country"
  t.integer "post_id", default: 0
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false

the link to:

<%= link_to "Remove post from user", remove_post_path, data: { confirm: "Are you sure?" } %>

the route:

get "user/remove_post", to: "accounts#remove_post", :as => : remove_post

the controller method:

def remove_post
  current_user.account.update_attributes(post_id: nil)

What i get in the console:

Account Update (0.6ms)  UPDATE "accounts" SET "post_id" = $1, "updated_at" = $2 WHERE "accounts"."id" = $3  [["post_id", 96], ["updated_at", "2019-02-27 10:43:53.432402"], ["id", 49]]

Maybe Try

ac = Account.first
ac.post_id = nil
ac.save(validate: false)

Rails: update an attribute to nil, Maybe Try ac = Account.first ac.post_id = nil ac.save(validate: false). Image Upload update_attributes setting column to nil I am working on a tutorial application, and I wanted to add image upload for user avatars. So, I generated a migration for the Users table and added avatar:string.

I went with this:

Account.update(current_user.account.id, post_id: nil)

Let me know if another solution might be better.

Nice way to set attributes to nil if blank - Rails, I've got a form for editing a model called Property. Property has two fields, web_color and thumbnail_path, that are nil by default, and the model� The current behavior in Rails is that thing.update_attributes (nil) will return and not update any attributes without throwing any error. However, Instead of allowing you to send a nil argument, it might be better to raise an ArgumentError which has a clear error message.

It seems that you have the relation between account and post. By default, Rails validates weather the relation exists. You can change this by adding optional: true to your model:

class Account < ApplicationRecord
  belongs_to :post, optional: true

After this, you can change successfully value of this column to nil.

rails 5 update attribute only if it's currently nil, rails update column rails update attribute without callbacks. What is the preferred way in Rails 5 with activerecord to update the attribute only if it is currently nil. (attribute) toggle (attribute). update_attribute (attribute, self [attribute]) end touch (*names, time: nil) Link Saves the record with the updated_at/on attributes set to the current time or the time specified.

update_attribute returns unexpected nil when new value is the same , update_attribute(name, value) returns nil if the submitted value is the same In rails 5 the unecessary update is skipped but I think the return value should be true . Return true if attribute is not changed for update_attribute Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records. Also note that. Validation is skipped. Callbacks are invoked. updated_at/updated_on column is updated if that column is available. Updates all the attributes that are dirty in this object.

Different ways to set attributes in ActiveRecord, * Mass Assignment Protection for attributes= is overridable. Further reading: update_attributes vs. update_attribute in ActiveRecord. Rails 4. Method, Uses Default� This method used to be called update_attributesin Rails 3. It changes the attributes of the model, checks the validations, and updates the record in the database if it validates. Note that just like update_attributethis method also saves other changed attributes to the database.

ActiveRecord::Persistence, Any change to the attributes on either instance will affect both instances. Initializes attribute to zero if nil and subtracts the value passed as by (default is 1). In Rails 5 release, attributes API was made public and allowed support for custom types. What is attribute API? Attribute API converts the attribute value to an appropriate Ruby type. Here is how the syntax looks like. attribute (name, cast_type, options) The first argument is the name of the attribute and the second argument is the cast type.