我有3個機型:Runners
,Jobs
和Surveys
。 Runner
模型has_many
工作。 Job
型號has_one
Survey
。我正在嘗試獲得所有跑步者調查(所有與屬於特定跑步者的工作相關的調查)。導軌有效記錄內部連接不工作
這裏是我的模型
runner.rb
class Runner < ActiveRecord::Base
has_many :jobs
end
job.rb
class Job < ActiveRecord::Base
belongs_to :runner
has_one :survey
end
survey.rb
class Survey < ActiveRecord::Base
attr_accessible :service, :speed, :suggestion, :job_id
belongs_to :job
end
爲了獲取所有作業一個跑步者,我打開了軌道控制檯並嘗試運行這樣的命令。
runner = Runner.first
joined_table = Job.joins(:survey)
joined_table.where(runner_id: runner.id)
這看起來像它輸出正確的SQL,但每當我跑joined_table,它的作用是返回回Job.all
。它不返回Job和Survey的連接表。我也試過以下
joined_table = Job.all(:include => :survey)
joined_table = Job.all(:select => '*', :joins => :survey)
joined_table = Job.all(:joins => :assignment, :include => :survey)
這些都不3個工作要麼
這就是它。謝謝你,這麼容易! – user2158382