2

我正在创建与Rails Guides Patient-Appointment-Physician数据模型几乎相同的关联。一个用户有很多prospects通过prospect_subscription。但是,当尝试user.prospects在 rails 控制台中访问时,会引发以下错误:

Rails couldn't find a valid model for Prospects association. Please provide the :class_name option on the association declaration. If :class_name is already provided, make sure it's an ActiveRecord::Base subclass. (NameError)

uninitialized constant User::Prospects (NameError)

这很奇怪,因为所有三个模型都在那里。迁移已经运行并且样本数据已经被填充并且可以在 pgAdmin 中检查。为什么 Rails 找不到模型?

模型中定义的关联如下:

models/prospect.rb

class Prospect < ApplicationRecord
  has_many :prospect_subscriptions
  has_many :users, through: :prospect_subscriptions
 end

models/user.rb


class User < ApplicationRecord
  has_many :prospect_subscriptions
  has_many :prospects, through: :prospect_subscriptions
end

models/prospect_subscription.rb
class ProspectSubscription < ApplicationRecord
  belongs_to :user
  belongs_to :prospect
end
4

0 回答 0