Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing this Error: NameError (uninitialized constant ActiveRecord::ConnectionAdapters::Mysql2Adapter::Column)

There are two apps. One of the apps is using rails 4.1.2. The other app is using rails 5.0.1. Here is how the rails 5 app works: it checks if the user is logged in:

  • If the user is logged in: then the user should be able to proceed forward.
  • If the user is NOT logged in, then it redirects them to the rails 4 app (which is where the user can log in). Once the user is logged in with the rails 4 app: the user should be able to access the rails 5 app no problem.

I am currently using the following gems in my Rails 5 project:

gem 'mysql2', '~> 0.4.4'
gem 'activerecord-session_store', '~> 1.0'

I then have the following in my config/initializers/session_store.rb file:

Rails.application.config.session_store :active_record_store, :key => ‘SOME_KEY’

Currently: if the user is not logged in: then it successfully redirects the user. The issue is when the user is logged in. I get this error when the logged in user attempts to access the rails 5 app:

NameError (uninitialized constant ActiveRecord::ConnectionAdapters::Mysql2Adapter::Column)
  • I do notice that the app is successfully connecting to the database
  • The app also appears to access the sessions table on initial request (when the user is not logged in).

Any suggestions on what is triggering this error and how to fix it? Ultimately: it almost appears as though rails 5 is incompatible with activerecord-session_store and mysql2.

like image 752
Neil Avatar asked Feb 17 '17 20:02

Neil


2 Answers

We were eventually able to figure it out.

The Marshal module can serialize ruby objects to the database. It can also deserialize stuff from the database in order to recreate the objects.

When you serialize an object, there appears to be some low level info that goes in there too, like the mysql2 connection adapter.

When the Rails 5 app tries to deserialize the data, it throws this error because the constant that exists in the MySql2 connection adapter for Rails 4 does not exist in the Rails 5 version of that adapter.

Our work around was to just not store or retrieve any of the serialized objects from the sessions table for our rails 5 app. That did the trick.

If we had really needed to retrieve serialized objects from the sessions table for our rails 5 app: then I think we would have had to come up with a custom solution.

Hope this helps others in the future!

like image 185
Neil Avatar answered Nov 06 '22 16:11

Neil


It may be due to changes in release i.e. Rails 5.0.

As In Ruby on Rails 5.0 Release Notes:

Removed support for the legacy mysql database adapter from core. Most users should be able to use mysql2. It will be converted to a separate gem when we find someone to maintain it.

Deprecated passing arguments to #tables - the #tables method of some adapters (mysql2, sqlite3) would return both tables and views while others (postgresql) just return tables. To make their behavior consistent, #tables will return only tables in the future.

Difference in constant as you described in your answer may be due to they going to change gem for mysql2 adapter.

like image 26
Rohit Dhiman Avatar answered Nov 06 '22 17:11

Rohit Dhiman