【发布时间】:2016-12-12 23:21:58
【问题描述】:
我想检索申请人的 application.applicant.profile.first_name,但我无法使用上面的申请人检索个人资料属性:first_name。
个人资料,应用程序通过外键连接:user_id到用户。有人可以建议一种方式吗?
这是我的联想:
用户.rb
class User < ApplicationRecord
has_many :applications
has_one :profile, -> { where role: 'user' }, dependent: :destroy
profile.rb
class Profile < ApplicationRecord
belongs_to :user, :class_name => 'User', :foreign_key => 'user_id'
job.rb
class Job < ApplicationRecord
has_many :applications, :dependent => :destroy
has_many :applicants, :through => :applications, :class_name => 'User'
应用程序.rb
class Application < ApplicationRecord
belongs_to :job
belongs_to :applicant, :class_name => 'User', :foreign_key => 'user_id'
【问题讨论】:
标签: sql ruby-on-rails ruby-on-rails-5 model-associations