Ya.
Ya.

Reputation: 2627

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?

Upvotes: 4

Views: 5552

Answers (3)

frostymarvelous
frostymarvelous

Reputation: 2805

As someone who always uses the bang methods, I recently had a similar issue and could not debug it. The only thing is, I had upgraded to rails 7.

Yup, so apparently, using return inside of a transaction block is no longer supported and will result in a rollback.

GITHUB: Deprecate committing a transaction exited with return or throw #29333

Upvotes: 0

IAmNaN
IAmNaN

Reputation: 10582

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.

Upvotes: 5

spullen
spullen

Reputation: 3317

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

Upvotes: 12

Related Questions