我有两个模型,有两个相应的表:ReferralRequest和Gender。在ReferralRequest表中,我存储了名为patient_gender_id和preferred_gender_id的性别的整数ID。 ids来自性别表。我希望能够调用类似于ReferralRequest.last.patient_gender.name的内容来返回genders表中的字符串而不是referral_requests表中的整数id,但它不起作用。我究竟做错了什么?所有帮助都对这位新秀表示赞赏。谢谢!
ReferralRequest
O(|P|)
性别
P: pattern you are searching for.
和相应的表格:
class ReferralRequest < ApplicationRecord
belongs_to :user
belongs_to :gender
has_many :referral_request_concerns
has_many :concerns, through: :referral_request_concerns
has_many :referral_request_insurances
has_many :insurances, through: :referral_request_insurances
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :content, presence: true, length: { minimum: 20 }
validates :patient_gender_id, presence: true
validates :preferred_gender_id, presence: true
end
答案 0 :(得分:2)
@gender = Gender.find(ReferralRequest.last.patient_gender_id)
或
class ReferralRequest < ApplicationRecord
belongs_to :patient_gender, :class_name => "Gender", :foreign_key => "patient_gender_id"
end
@gender = ReferralRequest.last.patient_gender.name
答案 1 :(得分:0)
您无法调用getUser()
,因为您的数据库表中没有patient_gender列。你可以致电ReferralRequest.last.patient_gender.name
。