i have 2 models:
question (id,text,section,factor)
answer (id,question_id,selected)
question has_many :answers answer belongs_to :question
my goal find factor of question text equal answer's selected field , search answers
to show mean here sql gives expected result
select questions.factor answers inner join questions on questions.text = answers.selected answers.id = 5 in rails try use joins
answers.joins(:question).where('questions.section = 4').where ('answers.selected = questions.text') but results not exact, can see in console rails includes connection related field "questions"."id" = "answers"."question_id"
select "answers".* "answers" inner join "questions" on "questions"."id" = "answers"."question_id" "answers"."participant_id" = $1 , (questions.section = 4) , (answers.selected = questions.text) well, believe charm of ruby should work here, please advice brilliant way wanted result.
thanks.
using answers.joins(:question) build generate inner join "questions" on "questions"."id" = "answers"."question_id" because how belongs_to :question association defined in answer.
but can pass string join condition need. (you might need additional where id = 5 condition):
answers.joins('inner join questions on questions.text = answers.selected')
Comments
Post a Comment