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

5.8k views Asked by At

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

3

There are 3 answers

1
Valor_ On BEST ANSWER

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

1
Rubyrider On

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

3
tala On

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