简体   繁体   中英

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

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

在此处输入图像描述

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

这是一个常见的 RubyMine 错误,通常可以通过运行File / Invalidate Caches...来解决。

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM