1

My model looks like this, nothing fancy:

# Table name: invoices
#
#  id               :integer         not null, primary key
#  total            :float
#  discount         :float
#  description      :text
#  created_at       :datetime        not null
#  updated_at       :datetime        not null
#  company_id       :integer
#  secret           :string(255)
#  address_id       :integer
#  price_per_credit :float
#  paid             :boolean

I can create instances of this both on the server and in the rails console, and these instances return true if I call .valid? on them (I turned off all validation for testing purposes).

1.9.3p194 :001 > i = Invoice.new
 => #<Invoice id: nil, total: nil, discount: nil, description: nil, created_at: nil, updated_at: nil, company_id: nil, secret: nil, address_id: nil, price_per_credit: nil, paid: nil> 
1.9.3p194 :002 > i.valid?
 => true

However, for some reason, I can't save these instances to the database.

1.9.3p194 :003 > i.save
   (0.3ms)  begin transaction
   (0.1ms)  rollback transaction
 => false

Locally I'm using sqlite, the production server is running MySQL, but both show this behavior. I'm not really sure what to do to debug this. I did run db:migrate, and my gems are up-to-date. How can I sove this?

2
  • 1
    Try save!. Perhaps you'll get more info this way. Commented Jul 2, 2012 at 9:29
  • 1
    Yep, that helped a lot. I got more output, including ActiveRecord::RecordNotSaved: ActiveRecord::RecordNotSaved. That was a great hint, Google led me here: apidock.com/rails/ActiveRecord/RecordNotSaved. One of my before_create filters was setting invoice.paid to false, but returned false too, causing the save proces to halt. Simply adding nil to the method solved the problem. Commented Jul 2, 2012 at 9:36

1 Answer 1

8

It turned out I had a before_create filter that returned false, causing the save proces to halt. To solve this, I added nil to the class, like this:

# BEFORE:
def set_paid
    self.paid = false
end

# AFTER:
def set_paid
    self.paid = false
    nil
end

Hope this helps others too!

Sign up to request clarification or add additional context in comments.

1 Comment

Yup. Was having same issue today :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.