仅当尝试通过单向方式将记录添加到“has_many:”时才会出现NameError

时间:2017-02-01 17:24:01

标签: ruby-on-rails activerecord

我可以使用location.doctor_profiles << doctor_profile将医生添加到地点但是我在尝试向医生添加位置时收到以下错误doctor_profile.locations << location

NameError: undefined local variable or method `doctor_profile' for #<Location>

该关联似乎工作正常,除了这个曾经向doctor_profile添加位置的实例

class DoctorProfile
  has_many :doctor_locations
  has_many :locations, through: :doctor_locations

class DoctorLocation
  belongs_to :doctor_profile
  belongs_to :location

class Location
  has_many :doctor_locations
  has_many :doctor_profiles, through: :doctor_locations

1 个答案:

答案 0 :(得分:0)

我仔细检查了导轨ActiveRecord关联指南,您的设置看起来很好。

您是否在数据库中添加了关联的ID?