繁体   English   中英

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

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

我正在编写一些教程,但遇到了一些问题。 RubyMine 找不到“:用户”关联的关联 Rails 模型失败

我在用着:
- 红宝石矿 7
- Ruby 版本 meneger (rvm)
- ruby​​-1.9.3-p551 [x86_64]
- ruby​​-2.1.5 [x86_64]
- 导轨导轨 4.1.8

- 宝石 sqllite3

我的模型是:

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

在此处输入图像描述

谢玛.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

我的代码有问题还是 RubyMine 配置有问题?

更新:
如果我使用 Mysql2 或 sqllite3 gem,问题是一样的

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

看起来问题出在 RubyMine 编辑器中(错误)! 如果您在 ruby​​-2.1.5 上运行您的应用程序并将您的 RubyMine 设置(Ruby SDK 和 gems)更改为其他版本,然后再更改回 ruby​​-2.1.5 版本,您将收到此错误。

快速修复是您创建一个新项目并将这些文件复制粘贴到那里

都说它没有找到在当前模型上形成用户关联所需的引用字段。

因此,您最好检查一下,如果您已经添加了列以将用户引用到这些表中,或者您已经这样做了,请再次尝试重新检查,如果您运行迁移以对数据库进行更改。

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

或生成以下迁移:

rails g migration AddCompanyRefToUsers user:references

另一方面,它可能是 ruby​​mine 的缓存问题。 # 我不知道

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

暂无
暂无

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

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