0
我能夠向醫生添加到使用location.doctor_profiles << doctor_profile
但是我收到以下錯誤試圖位置添加到醫生時位置doctor_profile.locations << location
NameError:「through`單程
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
我想你只需要location.doctor_profiles.first,因爲它是一個has_many_through。 –