Last Updated: February 25, 2016
·
896
· karunamon

Using ActiveRecord in non-Rails projects in Rubymine

If you've ever tried using the ActiveRecord tools in the RubyMine IDE, you might have run into a rather annoying problem where your model associations are not correctly seen and therefore throw bogus warnings. All you can really do is either ignore them or turn off the "Unresolved rails reference" inspection.. which is bad, because this is probably the single most important one in the app (protects you from various typos).

What the error looks like

If you're like me, this is downright infuriating.

There's an active bug report in to Jetbrains about this, but it's been sitting for a while. In the meantime, there's this workaround. Apparently RubyMine can't resolve these references correctly unless it thinks its working on an actual Rails application.

You can "trick" it by following these steps:

  • Move your models into their own .rb files, per model, just like there would be in a real Rails app. Put these under an app/models folder under your project's root directory.

  • Make a config directory under your project's root directory, and create two empty files, application.rb and routes.rb

  • Exit RubyMine. In your project's root folder, delete the hidden folder .idea. WARNING: This will delete any project-specific settings!

  • Restart and reopen your project. You should see the boilerplate rails server options in the run box at the top of the screen. Your models should now associate correctly.