저는 Runners
, Jobs
및 Surveys
의 3 가지 모델이 있습니다. Runner
모델 has_many
작업. Job
모델 has_one
Survey
. 나는 Runner (특정 주자에 속하는 일과 관련된 모든 설문 조사)에 대한 모든 설문 조사를 얻으려고합니다. 여기레일즈 활성 레코드 내부 조인이 작동하지 않습니다.
는
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 and Survey의 조인 된 테이블을 반환하지 않습니다. 또한 다음과 같은
joined_table = Job.all(:include => :survey)
joined_table = Job.all(:select => '*', :joins => :survey)
joined_table = Job.all(:joins => :assignment, :include => :survey)
없음을 시도하지 이러한 난 당신이
Survey.joins(:job).where(jobs: { runner_id: runner.id })
이 당신에게 주자에 속하는 작업에 속한 모든 Survey
객체를 제공해야합니다 믿고 3 일 중 하나
그게 전부입니다. 고마워요, 너무 쉬워요 !! – user2158382