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?
save!. Perhaps you'll get more info this way.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 settinginvoice.paidto false, but returnedfalsetoo, causing the save proces to halt. Simply addingnilto the method solved the problem.