Understand ActiveRecord::ReadOnlyRecord error
When you load a record with find options that have SQL fragments in :select
or :joins
, ActiveRecord will make that record read-only. This is a protective measure by Rails because such a record might have some additional attributes that don't correspond to actual table columns.
You can override that precaution by appending :readonly => false
to affected find options or scope options.
Related cards:
ActiveRecord::RecordNotFound errors allow you to query the :name and :id of the model that could not be found
ActiveRecord::RecordNotFound
errors provide quite meaningful error messages that can provide some insight on application details. Consider the following:
ActiveRecord::RecordNotFound: Couldn't find Organisation::Membership with 'id'=12 [WHE...
Rails: How to check if a certain validation failed
If validations failed for a record, and you want to find out if a specific validation failed, you can leverage ActiveModel's error objects.
You rarely need this in application code (you usually just want to print error messages), but it can be u...
ActiveRecord: Overwriting a setter causes trouble with attributes depending on each other
Undeterministically I got a nil
error on saving the object caused by the random order of hash elements of the params hash.
class Feedback
belongs_to :user
def role=(value)
@role = value
self.email = find...
ActiveRecord::StatementInvalid: Mysql2::Error: closed MySQL connection
I recently experienced the error ActiveRecord::StatementInvalid: Mysql2::Error: closed MySQL connection
. Apparently this happens when there is a timeout during query execution. In order to fix this you can reconnect to your db.
Therefore either...
Mysql::Error: SAVEPOINT active_record_1 does not exist: ROLLBACK TO SAVEPOINT active_record_1 (ActiveRecord::StatementInvalid)
Possible Reason 1: parallel_tests - running more processes than features
If you run old versions of parallel_tests with m...
How to not die with ActionView::MissingTemplate when clients request weird formats
When HTTP clients make an request they can define which response formats they can process. They do it by adding a header to the HTTP request like this:
Accept: application/json
This means the client will only understand JSON respon...
How to load an SQL dump from a migration
If you want to load an SQL dump from an ActiveRecord migration, you might find this to be harder than you thought. While you can call ActiveRecord::Base.connection.execute(sql)
to execute arbitrary SQL commands, the MySQL connection is configure...
Dealing with I18n::InvalidPluralizationData errors
When localizing model attributes via I18n you may run into errors like this:
I18n::InvalidPluralizationData: translation data { ... } can not be used with :count => 1. key 'one' is missing.
They seem to appear out of the blue and the ...
How Rails chooses error pages (404, 500, ...) for exceptions
When your controller action raises an unhandled exception, Rails will look at the exception's class
and choose an appropriate HTTP status code and error page for the re...