postgresql - Rails - Active Record Query Not Querying on Variable Assignment (Want to Load Eagerly) -
this query returns question submissions have rubric_item_evaluations present=true , has rubric item @rubric_item.id
submissions_to_update = questionsubmission.joins(:rubric_item_evaluations).where(:rubric_item_evaluations => {:present => true , :rubric_item_id => @rubric_item.id})
then destroy rubric item
@rubric_item.destroy
then iterate through submissions regrade
submissions_to_update.each |submission| submission.grade! end
but reason question_submissions_update gets queried afterwards on .each! , time doesn't query right thing (returning empty array). i'm confused... why doesn't query on initial variable assignment?
so in summary query seems happen here
submissions_to_update = questionsubmission.joins(:rubric_item_evaluations).where(:rubric_item_evaluations => {:present => true , :rubric_item_id => @rubric_item.id}) @rubric_item.destroy # query submissions_to_update happening after destroy rubric item submissions_to_update.each |submission| submission.grade! end
i can show sql statements if necessary!
update necessary actual execute query before destroying rubric item.
when write this:
submissions_to_update = questionsubmission.joins(:rubric_item_evaluations).where(:rubric_item_evaluations `=> {:present => true , :rubric_item_id => @rubric_item.id})
you building activerecord::relation, query in active record format in end building query not executing it.
then said right after building query destroy @rubric_item, query built become wrong afterwards. @rubric_item.id not anymore in database join query not find record.
you need retrieve questions before destroying item in fact need execute regrading process before destroying rubric_item such as:
submissions_to_update = questionsubmission.joins(:rubric_item_evaluations).where(:rubric_item_evaluations => {:present => true , :rubric_item_id => @rubric_item.id}) submissions_to_update.each |submission| submission.grade! end @rubric_item.destroy
Comments
Post a Comment