Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic rollback without any error?

I'm unable to create a row in the DB. Rails apparently starts and then immediately rolls back the transaction without any errors. I'm using sqlite3.

  logger.debug("creating billing name...")
  BillingName.create() #also tried BillingName.new.save
  logger.debug("...created")

Log file:

 creating billing name...
 ^[[1m^[[36m (0.1ms)^[[0m  ^[[1mbegin transaction^[[0m
 ^[[1m^[[35m (0.1ms)^[[0m  rollback transaction
 ...created

select * from billing_name shows indeed no entry has been added. How can I tell why the transaction is being rejected?

like image 600
Ya. Avatar asked Dec 02 '22 22:12

Ya.


2 Answers

You can check the errors after a save or valid?

billing_name = BillingName.new
billing_name.save # or billing_name.valid?
puts billing_name.errors.inspect
like image 88
spullen Avatar answered Jan 01 '23 18:01

spullen


These are good answers that help you find the error by inspecting the model. I use those too.

But I've found that ActiveRecord doesn't always provide useful information, especially in callbacks. valid? will be true, errors will be empty, and the console won't show anything except the rollback. So, a good place to look is what is happening in those callbacks, specifically the before_create filter.

like image 21
IAmNaN Avatar answered Jan 01 '23 19:01

IAmNaN