Web Development

Weird Stuff eh? The ActiveRecord::ReadOnlyRecord: ActiveRecord::ReadOnlyRecord problem arises whenever you use a lot of SQL in your ActiveRecord queries.

Something like this will prompt that:

a = Product.joins('LEFT JOIN users ON products.user_id = users.id').where('products.user_id IS NULL')
a.destroy_all

This will immediately produce this error:

(0.2ms) BEGIN
(25.6ms) ROLLBACK
ActiveRecord::ReadOnlyRecord: ActiveRecord::ReadOnlyRecord

Solution

Use ActiveRecord.

a = Product.includes(:user).where('products.user_id IS NULL')
a.destroy_all

Good luck!