嗨,我只创建了一个与其他人的关系模型,但我对rails的多元化选项感到惊讶。我是说。
我创建了这样的模型:
rails g model Report name:string....就像我对:
rails g model Patient name:string...
rails g model Doctor name:string....医生有很多病人,所以我可以去控制台输入:
patient.doctor => gives me the doctor from a patient
doctor.patients => gives me all patients from a doctor (note patients in plural)奇怪的是,我对report也做了同样的事情,我希望得到命令:
patient.reports (note plural)但是,如果我想检索病人报告,我必须这样做:
patient.report (note singular)... AND IT WORKS!有人能照亮我的失明吗?
发布于 2013-08-15 15:50:01
检索相关对象的方法取决于您在模型中声明它的方式。
下面是一些例子:
class Patient < ActiveRecord::Base
belongs_to :doctor # singular
end
class Doctor < ActiveRecord::Base
has_many :patients # plural
end然后你就可以:
patient.doctor # => return the associated doctor if exists
doctor.patients # => return the patients of this doctor if exist我想你已经用单数声明了你的关系:
# What I think you have
class Patient < ActiveRecord::Base
has_many :report
end但你应该在这里用复数:
# What I think you should use
class Patient < ActiveRecord::Base
has_many :reports
^
# Make it plural
endhttps://stackoverflow.com/questions/18256114
复制相似问题