RubyMine error: Unable to find associated Rails Model for ':users' associations failed
Asked Answered
M

3

6

I'm working on some tutorial and i'm having some problems. RubyMine can't find associated Rails Model for ':users' associations failed

I'm using:
- RubyMine 7
- Ruby version meneger (rvm)
- ruby-1.9.3-p551 [ x86_64 ]
- ruby-2.1.5 [ x86_64 ]
- rails Rails 4.1.8

- Gem sqllite3

My models are:

class Company < ActiveRecord::Base
  has_many :users
  has_many :projects
end

class Project < ActiveRecord::Base
  belongs_to :company
  has_many :works
  has_many :users, :through => :works
end

class User < ActiveRecord::Base
  belongs_to :company
  has_many :works
  has_many :projects, :through => :works
end

class Work < ActiveRecord::Base
  belongs_to :project
  belongs_to :user
end

enter image description here

Shema.rb

ActiveRecord::Schema.define(version: 20141207111312) do

  create_table "companies", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "projects", force: true do |t|
    t.string   "name"
    t.integer  "company_id"
    t.integer  "default_rate"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.string   "fname"
    t.string   "lname"
    t.integer  "company_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id"
  end

  add_index "users", ["user_id"], name: "index_users_on_user_id"

  create_table "works", force: true do |t|
    t.integer  "project_id"
    t.integer  "user_id"
    t.datetime "datetimeperformed"
    t.decimal  "hours",             precision: 5, scale: 2
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

Is there something wrong with my code or is this some wrong RubyMine configuration?

UPDATE:
Problem is the same if i use Mysql2 or sqllite3 gem

Mink answered 7/12, 2014 at 10:16 Comment(3)
check my answer. if you don't have those referentials for the association. You can do that following my answer.Deepsix
Also please add your schema.rb file in your question.Deepsix
I have updated a questionMink
M
4

Looks like the problem is in RubyMine editor (bug)! If you run your application let's say on ruby-2.1.5 and you change you RubyMine setting (Ruby SDK and gems) to some other version and then change back to ruby-2.1.5 version, you will get this error.

Quick fix is that you create a new project and copy paste those files there

Mink answered 25/1, 2015 at 13:12 Comment(2)
Thanks for the tip. I've removed .idea directory from the project then added project once again to the RubyMine. Another thing worth to try is to add "ruby-" prefix to the .ruby-version file so gemset can be detected automatically.Russianize
Invalidate caches is a lighter weight fix as noted in https://mcmap.net/q/1607729/-rubymine-error-unable-to-find-associated-rails-model-for-39-users-39-associations-failedHagberry
B
13

This is a common RubyMine error and can often be resolved by running Invalidate Caches....

Bonaire answered 6/4, 2017 at 15:49 Comment(4)
I think this option doesn't exist anymore. However, I used File -> Repair IDE and it helped.Stilted
@Stilted Still exists as of RubyMine 2022.1.2, though it's now just "Invalidate Caches..."Bonaire
Yes, you're right!Stilted
I can confirm this works, but only partially. My previous has_many associations work, but now some belongs_to ones do not.Hagberry
M
4

Looks like the problem is in RubyMine editor (bug)! If you run your application let's say on ruby-2.1.5 and you change you RubyMine setting (Ruby SDK and gems) to some other version and then change back to ruby-2.1.5 version, you will get this error.

Quick fix is that you create a new project and copy paste those files there

Mink answered 25/1, 2015 at 13:12 Comment(2)
Thanks for the tip. I've removed .idea directory from the project then added project once again to the RubyMine. Another thing worth to try is to add "ruby-" prefix to the .ruby-version file so gemset can be detected automatically.Russianize
Invalidate caches is a lighter weight fix as noted in https://mcmap.net/q/1607729/-rubymine-error-unable-to-find-associated-rails-model-for-39-users-39-associations-failedHagberry
D
0

Its all saying that its not found the referential fields which are required to form users association on the current model.

So you better check, if you already added the columns to reference users to those tables or if you already did that, try recheck once again, if you run the migration to make the changes done on database.

# example
add_reference :users, :company, index: true

or generate the following migration:

rails g migration AddCompanyRefToUsers user:references

On other side, it could be rubymine's caching problem. # I am not sure

Reference: http://edgeguides.rubyonrails.org/active_record_migrations.html

Deepsix answered 7/12, 2014 at 10:50 Comment(1)
Sorry, but this didn't fixed the problem... thx for help anywayMink

© 2022 - 2024 — McMap. All rights reserved.