Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to set up a new Rails 5 application (ruby 2.3.1, rails 5.0.0.rc1) with postgresql, devise gems and it is failing to run rails db:seed due to following error:

PG::UndefinedTable: ERROR:  relation "application_records" does not exist
LINE 8:                WHERE a.attrelid = '"application_records"'::r...
/Users//.rvm/gems/[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/postgresql/database_statements.rb:88:in `async_exec'
/Users/foo/.rvm/gems/[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/postgresql/database_statements.rb:88:in `block in query'
/Users/foo/.rvm/gems/[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
/Users/foo/.rvm/gems/[email protected]/gems/activesupport-5.0.0.rc1/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/foo/.rvm/gems/[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract_adapter.rb:560:in `log'
/Users/foo/.rvm/gems/r[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/postgresql/database_statements.rb:87:in `query'
/Users/foo/.rvm/gems/[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/postgresql_adapter.rb:739:in `column_definitions'
/Users/foo/.rvm/gems/[email protected]/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:227:in `columns' 

After much googling, I've realized that this has something to do with the ApplicationRecord base class change in rails 5. Clearly there's no table called application_records and so active_support shouldn't be looking for it. I have already checked that app/models/application_record.rb exists and has the right content. Also, the User model (which is the only model in my app currently) extends ApplicationRecord as expected:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, 
         :recoverable, :rememberable, :trackable, :validatable

rails db: migrate runs fine, but rails db:seed chokes with the above error.

Can anyone shed some light on what might be causing this?

Contents of application_record.rb:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
                Thanks. I created a sample 5.0 RC app but was not able to reproduce this issue. I would advice to open an issue on Rails issue tracker - github.com/rails/rails/issues -  with a sample application uploaded to Github which reproduces the issue. Tag me as @prathamesh-sonpatki on Github issue. Thanks.
– Prathamesh Sonpatki
                Jun 12, 2016 at 17:33
                Thanks @PrathameshSonpatki. I have uploaded my project on github and filed a bug as you suggested: github.com/rails/rails/issues/25379. I've also found this workaround: If I add self.table_name = 'users' in my User model, everything works fine.
– paneer_tikka
                Jun 13, 2016 at 6:26
                This was my issue. I had to downgrade my RoR app from 3 databases to 1 in order to deploy to Heroku. When doing that I got too ambitious in removing lines in the models and removed self.abstract_class = true from app/models/application_record.rb. Thank you @agbodike for this solution!
– Christopher Warrington
                Sep 23, 2020 at 3:20

Answering My own question, so it may help others running into similar issues.

As metioned by @PrathameshSonpatki on the rails issue, this happens because userstamp gem injects relationships on ActiveRecord::Base rather than ApplicationRecord. (Note that userstamp gem isn't rails 5 compatible at the time of this writing).

If you have a gem not yet tested with rails 5 and you get an issue where active_support goes looking for a table called application_records, check if one of the gems you are using injects relationships on the ActiveRecord::Base class. Based on this blog post, the very purpose of ApplicationRecord is to avoid global injection of ActiveRecord::Base. Therefore you'd need to look for a version of such gem that is compatible with Rails 5 or patch such gem to inject the necessary behavior into ApplicaitonRecord instead of ActiveRecord::Base.

I wonder what is the best way to track down a gem which breaks our app. We have few hundreds and userstamp is not in there. – lzap Apr 12, 2017 at 8:26 @Izap I have a gem called gem_bench, which allows you to grep through the source code of all gems bundled in a project, so you can look for things like that. github.com/pboling/gem_bench – Peter H. Boling Jul 24, 2019 at 5:56

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.