Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8047011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:58:15+00:00 2026-06-05T05:58:15+00:00

UPDATE: If the after_validation callback is used, it works as desired (e.g. the false

  • 0

UPDATE:
If the after_validation callback is used, it works as desired (e.g. the false value is persistent). Still would like to know why that is, but I guess this is solved for my purposes 🙂

For a boolean field, I would like a callback in the model to set the default value to false instead of nil.

Currently when I create a new record, it initially shows the value as false, but then shows it as nil.

Wondering what’s going on here and if the desired behavior is possible w/ a callback.

This is in the model:

  after_save :default_is_forsale

  def default_is_forsale
    self.not_for_sale = false if self.not_for_sale.nil?
  end

Here is the rails console output (irrelevant bits omitted):

1.9.3p125 :001 > Item.create(name: "thing 4")
   (0.1ms)  begin transaction
  SQL (6.4ms)  INSERT INTO items [...]
   (190.8ms)  commit transaction
 => #<Item id: 20, name: "thing 4", not_for_sale: false> 

Cool, created the new record with a default value of false. But when I check again:

1.9.3p125 :002 > Item.last
  Item Load (0.3ms)  SELECT [...]
 => #<Item id: 20, name: "thing 4", not_for_sale: nil> 

Weird, now the value is nil.

But if I create a new record and explicitly set the value to false, it acts as I’d expect:

1.9.3p125 :003 > Item.create(name: "more thing", not_for_sale: false)
   (0.1ms)  begin transaction
  SQL (0.7ms)  INSERT INTO items [...]
   (225.2ms)  commit transaction
 => #<Item id: 21, name: "more thing", not_for_sale: false> 

When retrieved, the record still shows its boolean value of false

1.9.3p125 :004 > Item.last
  Item Load (0.3ms)  SELECT [...]
=> #<Item id: 21, name: "more thing", not_for_sale: false> 

BTW, I read elsewhere that the desired result is achievable via db migrations, but I am new to rails and would like to accomplish it through the model.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:58:17+00:00Added an answer on June 5, 2026 at 5:58 am

    Change your migration to set this boolean to false, as default. if there was code i’d show you.

    I just read you were ‘new to rails’ but that doesn’t matter. You don’t need to do it in the model, unless you want that record to be true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After I click update in the grid view, the code works successfully. But when
Update: After some more reading I see that this problem is totally general, you
I'm trying to write an after update trigger that does a batch update on
I released an update early this morning and my versioning was previously like this:
Background I, like scores of programmers before me, am working on an application that
I have form autocomplete code that executes when value changes in one textbox. It
This is partly a continuation from this thread: jquery - return value from callback
Im starting to build a new app and I would like to use Backbone
I have a form with user details and an update statement that will update
I have created asp.net WebService. I want to update user's info after validating him

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.