簡體   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