用户未定义的方法`belongs_to'(无数据库连接):Class(NoMethodError)

时间:2013-12-30 03:10:58

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord

您好我正在尝试在postgresDB中获得以下关系。

这是我的关系 公司has_many评级,:通过=> :用户 评级has_one:公司,:通过=>:用户 用户belongs_to:公司 用户belongs_to:rating

为了获得这个,我在我的数据库中创建了以下迁移

class AddCompanyIdToUser < ActiveRecord::Migration
  def change
    add_column :users, :company_id, :integer
  end
end

class AddRatingIdToUser < ActiveRecord::Migration
  def change
    add_column :users, :rating_id, :integer
  end
end

并将关系添加到我的模态

运行服务器时出现以下错误

`method_missing': undefined method `belong_to' for User(no database connection):Class (NoMethodError)

我是rails的新手。任何人都可以告诉我我的问题是什么。做正确的迁移吗?

1 个答案:

答案 0 :(得分:3)

您要查找的方法是belongs_to,而不是belong_to。没有belong_to因此您的user.rb应该更像这样:

class User < ActiveRecord::Base
  belongs_to :companies
  belongs_to :rating
  #-----^