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 8707401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:51:45+00:00 2026-06-13T03:51:45+00:00

I’m writing a simple blog app in Rails, version 3.2.8. I’ve scaffolded the posts

  • 0

I’m writing a simple blog app in Rails, version 3.2.8. I’ve scaffolded the “posts” resource as follows:

rails g scaffold post title:string body:string islive:boolean

I want to set islive to be false by default and I’m trying to do so in my model. I also decided I don’t want it available to mass assignment so I’ve coded up my model as follows:

class Post < ActiveRecord::Base
  attr_accessible :body,  :title

  before_save :default_values

  def default_values
    self.islive = false
  end
end

I’ve also removed the islive field from my form. The trouble is when I try to create a new post with the model as above, it doesn’t save. The console looks like this:

Started POST "/posts" for 127.0.0.1 at 2012-10-14 18:23:30 +0100
Processing by PostsController#create as HTML
  Parameters: {"utf8"=>"✓",
"authenticity_token"=>"/lNa21JBeBz/H2cYLqAsF8uWr1frLV88WsOVKzOqwb4=", "post"=>
 {"title"=>"sample title", "body"=>"the body"}, "commit"=>"Create Post"}
(0.1ms)  begin transaction
(0.0ms)  rollback transaction
Rendered posts/_form.html.erb (1.8ms)
Rendered posts/new.html.erb within layouts/application (2.5ms)
Completed 200 OK in 29ms (Views: 26.3ms | ActiveRecord: 0.1ms)

And nothing is saved. I simply get the begin transaction rollback transaction However, if I change default_values to this:

def default_values
    self.islive = 'false'
end

everything works and it saves fine, with a falsey value for islive. Even if I just do something like this:

def default_values
    self.islive = false
    Rails.logger.debug self.islive
end

then the save works.

Can somebody shed some light on why this behaves as it does?

  • 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-13T03:51:46+00:00Added an answer on June 13, 2026 at 3:51 am

    before_save and other callbacks terminate saving if they return false. This is exactly what you did. It’s a trap if you forget about it. You can put some true (or even nil) at the very last line:

    def default_values
      self.islive = false
      true
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I am writing an app with both english and french support. The app requests
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

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.