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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:46:40+00:00 2026-06-10T10:46:40+00:00

I am having trouble with Rails’ save method; it seems to be failing when

  • 0

I am having trouble with Rails’ save method; it seems to be failing when it should, but not succeeding when it should.

I have a Claim model, that can be saved with any Status, but if it is to be submitted (status_id == 5) then the terms and conditions have to be accepted.

validates :terms_and_conditions, :acceptance => {:accept => true, :if => :submitted?}

def submitted? # simplified for this example
  status_id == 5
end

However, I am also storing the time that the terms were accepted (under db field tnc_accepted_at), and defining terms_and_conditions as the presence of this field. (This bit works fine, I’m just not sure if it’s relevant to my problem.)

def terms_and_conditions
  tnc_accepted_at.present?
end

def terms_and_conditions=(bool) # browser will pass '0' or '1'
  self.tnc_accepted_at = bool.in?([false, nil, 0, '', '0']) ? nil : DateTime.now
end

But here’s the rub. The claim starts off in this state:

claim
 => #<Claim id: 51, tnc_accepted_at: nil, status_id: 4>
claim.valid?
 => true

Then I try to submit it:

claim.update_attributes! :status_id => 5
   (0.3ms)  BEGIN
  ClaimItem Load (1.5ms)  SELECT --blah blah blah
  Status Load (0.6ms)  SELECT --blah blah blah
   (0.4ms)  ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Terms and conditions must be accepted

… which is perfect, but when I try to correct the error:

claim.update_attributes! :terms_and_conditions => true
   (0.3ms)  BEGIN
  ClaimItem Load (1.1ms)  SELECT --blah blah blah
   (0.7ms)  UPDATE "claims" --blah blah blah
   (0.2ms)  ROLLBACK
 => nil

… which is just weird! And I’ve noticed that I also get this any time I try to save the record, under whatever set of circumstances (using save, save!, update_attributes, update_attributes!, :tnc_accepted_at => DateTime.now, it doesn’t matter) – if it’s valid, it rolls back and returns nil; if it’s invalid, the error is raised as you would expect.

  • 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-10T10:46:41+00:00Added an answer on June 10, 2026 at 10:46 am

    Naturally it’s something really simple, after I spent an entire day on it…

    So it seems update_attributes only works properly on records that have already been saved.

    > claim
     => #<Claim id: 51, tnc_accepted_at: nil, status_id: 4>
    > claim.valid?
     => true
    #=========
    > claim.save # This makes all the difference!
       (0.3ms)  BEGIN
      Claim Load (0.4ms)  SELECT --blah blah blah
       (1.0ms)  INSERT INTO "claims" --blah blah blah
       (14.8ms)  COMMIT
     => true
    #=========
    > claim.update_attributes! :status_id => 5
       (0.3ms)  BEGIN
      ClaimItem Load (1.5ms)  SELECT --blah blah blah
      Status Load (0.6ms)  SELECT --blah blah blah
       (0.4ms)  ROLLBACK
    ActiveRecord::RecordInvalid: Validation failed: Terms and conditions must be accepted
    # ... just as expected.
    > claim.update_attributes! :terms_and_conditions => true
       (0.3ms)  BEGIN
      ClaimItem Load (0.8ms)  SELECT --blah blah blah
       (0.8ms)  UPDATE "claims" --blah blah blah
       (11.4ms)  COMMIT
     => true
    # ... hooray!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails app that has asynchronous processing, and I'm having trouble getting
I am Rails newbie and having some trouble passing parameters to form. I have
I am having trouble accessing the correct information in my rails model (which I
I'm having trouble figuring out how best to model my data. I have the
I'm having trouble sending mail using SMTP from a Rails app. I have a
I'm a jquery/rails newbie and I'm having trouble using .sortable(). I have expandable rows
I have a couple of basic Rails problems that I'm having trouble finding relevant
I'm having trouble with something that should be so easy, but I don't know
I have an older application running Ruby on Rails 2.2, and I'm having trouble
I'm having trouble displaying a Rails form in a horizontal line. I have a

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.