我的原始活动记录查询显示在下面,并显示它产生的查询......
有效记录查询
player.measurables.includes(:workout, :measurable_type, [workout: :player_workout_type])
.where(measurable_type_id: measurable_type.id)
.order('player_workout_types.priority ASC, measurables.value DESC').first
结果
SELECT [measurables].[id] AS t0_r0, [measurables].[workout_id] AS t0_r1, [measurables].[measurable_type_id] AS t0_r2, [measurables].[value] AS t0_r3, [measurables].[created_at] AS t0_r4, [measurables].[updated_at] AS t0_r5, [measurables].[imported_with_errors] AS t0_r6, [measurables].[import_key] AS t0_r7, [measurables].[order_by] AS t0_r8, [measurables].[reliability_id] AS t0_r9, [workouts_measurables].[id] AS t1_r0, [workouts_measurables].[player_id] AS t1_r1, [workouts_measurables].[date] AS t1_r2, [workouts_measurables].[description] AS t1_r3, [workouts_measurables].[player_workout_type_id] AS t1_r4, [workouts_measurables].[nfl_player_id] AS t1_r5, [workouts_measurables].[college_player_id] AS t1_r6, [workouts_measurables].[import_key] AS t1_r7, [workouts_measurables].[created_at] AS t1_r8, [workouts_measurables].[updated_at] AS t1_r9, [workouts_measurables].[recorder_id] AS t1_r10, [workouts_measurables].[field_conditions] AS t1_r11, [workouts_measurables].[comments] AS t1_r12, [workouts_measurables].[jersey] AS t1_r13, [workouts_measurables].[report_id] AS t1_r14, [workouts_measurables].[all_star_game_id] AS t1_r15, [player_workout_types].[id] AS t2_r0, [player_workout_types].[type] AS t2_r1, [player_workout_types].[description] AS t2_r2, [player_workout_types].[rising_seniors_event] AS t2_r3, [player_workout_types].[import_key] AS t2_r4, [player_workout_types].[created_at] AS t2_r5, [player_workout_types].[updated_at] AS t2_r6, [player_workout_types].[priority] AS t2_r7, [measurable_types].[id] AS t3_r0, [measurable_types].[is_primary] AS t3_r1, [measurable_types].[code] AS t3_r2, [measurable_types].[abbreviation] AS t3_r3, [measurable_types].[description] AS t3_r4, [measurable_types].[unit_id] AS t3_r5, [measurable_types].[created_at] AS t3_r6, [measurable_types].[updated_at] AS t3_r7, [measurable_types].[preferred_type] AS t3_r8, [measurable_types].[order_by] AS t3_r9, [measurable_types].[precision] AS t3_r10, [reliabilities].[id] AS t4_r0, [reliabilities].[code] AS t4_r1, [reliabilities].[description] AS t4_r2, [reliabilities].[priority] AS t4_r3, [reliabilities].[created_at] AS t4_r4, [reliabilities].[updated_at] AS t4_r5, [reliabilities].[is_active] AS t4_r6, [reliabilities].[translation] AS t4_r7 FROM [measurables] LEFT OUTER JOIN [workouts] [workouts_measurables] ON [workouts_measurables].[id] = [measurables].[workout_id] LEFT OUTER JOIN [player_workout_types] ON [player_workout_types].[id] = [workouts_measurables].[player_workout_type_id] LEFT OUTER JOIN [measurable_types] ON [measurable_types].[id] = [measurables].[measurable_type_id] LEFT OUTER JOIN [reliabilities] ON [reliabilities].[id] = [measurables].[reliability_id] INNER JOIN [workouts] ON [measurables].[workout_id] = [workouts].[id] WHERE [workouts].[player_id] = @0 AND [measurables].[measurable_type_id] = @1 ORDER BY reliabilities.priority, player_workout_types.priority ASC, measurables.value DESC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY', N'@0 int, @1 int', @0 = 203646, @1 = 203
将NULL放在最后的活动记录查询
player.measurables.includes(:workout, :measurable_type, :reliability, [workout: :player_workout_type])
.where(measurable_type_id: measurable_type.id)
.order('case when reliabilities.priority is not null then 0 else 99 end ASC, player_workout_types.priority ASC, measurables.value DESC').first
结果
以下内容出错......
TinyTds::Error: The multi-part identifier "reliabilities.priority" could not be bound.:
EXEC sp_executesql N'SELECT [measurables].* FROM [measurables] INNER JOIN [workouts] ON [measurables].[workout_id] = [workouts].[id] WHERE [workouts].[player_id] = @0 AND [measurables].[measurable_type_id] = @1 ORDER BY case when reliabilities.priority is not null then 0 else 99 end ASC, player_workout_types.priority ASC, measurables.value DESC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY', N'@0 int, @1 int', @0 = 203646, @1 = 203
答案 0 :(得分:0)
我似乎错误地认为includes
是left join
。从评论中做出以下更正后,这对我有用..
player.measurables.includes(:workout, :measurable_type, :reliability, [workout: :player_workout_type], :reliability)
.joins('left join reliabilities on reliabilities.id = measurables.reliability_id')
.where(measurable_type_id: measurable_type.id)
.order('case when reliabilities.priority is null then 99 else reliabilities.priority end ASC, measurables.value ASC').first