简体   繁体   English

RubyMine 错误:无法找到“:用户”关联的关联 Rails 模型失败

[英]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 RubyMine 找不到“:用户”关联的关联 Rails 模型失败

I'm using:我在用着:
- RubyMine 7 - 红宝石矿 7
- Ruby version meneger (rvm) - Ruby 版本 meneger (rvm)
- ruby-1.9.3-p551 [ x86_64 ] - ruby​​-1.9.3-p551 [x86_64]
- ruby-2.1.5 [ x86_64 ] - ruby​​-2.1.5 [x86_64]
- rails Rails 4.1.8 - 导轨导轨 4.1.8

- Gem sqllite3 - 宝石 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谢玛.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?我的代码有问题还是 RubyMine 配置有问题?

UPDATE:更新:
Problem is the same if i use Mysql2 or sqllite3 gem如果我使用 Mysql2 或 sqllite3 gem,问题是一样的

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

Looks like the problem is in RubyMine editor (bug)!看起来问题出在 RubyMine 编辑器中(错误)! 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.如果您在 ruby​​-2.1.5 上运行您的应用程序并将您的 RubyMine 设置(Ruby SDK 和 gems)更改为其他版本,然后再更改回 ruby​​-2.1.5 版本,您将收到此错误。

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.另一方面,它可能是 ruby​​mine 的缓存问题。 # I am not sure # 我不知道

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RubyMine错误:无法运行gem&#39;rail&#39;。 找不到&#39;rails&#39; - RubyMine error: Unable to run gem 'rails'. Cannot find 'rails' Rails 4-链接模型关联以访问关联的方法 - Rails 4 - chaining model associations to access associated methods 尝试在测试中查找关联模型时出现Rails测试错误 - Rails test error when attempting to find associated model in test 如何在关联的Ruby on Rails模型中声明关联? - How to declare associations in a Ruby on Rails model associated to itself? Rails:从关联模型中获取所有用户 - Rails: Getting all users from an associated model rails查找模型而无需加载相关的关联 - rails find model without loading relevant associations Elastic Search Rails查找关联的模型属性 - Elastic Search Rails find associated model attributes Rails 3 HABTM通过记录关联模型属性查找 - Rails 3 HABTM find by record associated model attribute Rails协会-多个用户在单个模型中扮演不同的角色 - Rails Associations - Multiple users fulfilling different roles for single model Rails - Polymorphic has_many:通过关联 - &#39;无法在模型中找到源关联&#39;错误 - Rails - Polymorphic has_many :through associations - 'Can't find source association in model' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM