Reputation: 3062
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?
Upvotes: 1
Views: 489
Reputation: 3062
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!
Upvotes: 8