From ced19b6cbca62ef0ee817f5b3b9c4fba69b014eb Mon Sep 17 00:00:00 2001 From: "Billy.Zheng" Date: Fri, 21 Jun 2024 14:21:45 +0800 Subject: [PATCH] dirty workaround for fix left join two condition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 注意修改了 avram 源码,才可以编译, https://github.com/luckyframework/avram/pull/1049 --- src/actions/universities/index.cr | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/actions/universities/index.cr b/src/actions/universities/index.cr index 0c6ac60..3eaf2de 100644 --- a/src/actions/universities/index.cr +++ b/src/actions/universities/index.cr @@ -36,10 +36,10 @@ class Universities::Index < BrowserAction param page : Int32 = 1 get "/universities" do - user_chong_wen_bao_query = ChongWenBaoQuery.new.user_id(current_user.id) + cwb_query = ChongWenBaoQuery.new # 这里的 preload 是 lazy 的, 即, 它并不会立即 preload 所有的记录, 而是在最后 paginate # 的时候, 考虑实际的 where 条件, 仅 select 所需的记录. - query = UniversityQuery.new.preload_chong_wen_baos(user_chong_wen_bao_query).preload_city + query = UniversityQuery.new.preload_chong_wen_baos(cwb_query).preload_city if q.presence if q.matches? /^\d{4}$/ @@ -47,7 +47,7 @@ class Universities::Index < BrowserAction else query = query.where("(name &@~ ?", q) .or(&.where_chong_wen_baos( - ChongWenBaoQuery.new.where("chong_wen_baos.university_remark &@~ ?)", q), + cwb_query.where("chong_wen_baos.university_remark &@~ ?)", q), auto_inner_join: false ).left_join_chong_wen_baos) end @@ -61,9 +61,11 @@ class Universities::Index < BrowserAction query = query.batch_level(batch_level) if batch_level.presence - query = query.where_chong_wen_baos(user_chong_wen_bao_query.university_remark.is_not_nil) if is_exists_remark + cwb_query = cwb_query.where do |wh| + wh.is_excluded(false).or(&.is_excluded.is_nil) + end - cwb_query = user_chong_wen_bao_query.is_excluded(false) + cwb_query = cwb_query.university_remark.is_not_nil if is_exists_remark if is_marked_2023 || is_marked_2022 || is_marked_2021 || is_marked_2020 || is_marked || chong_2023 || chong_2022 || chong_2021 || chong_2020 || @@ -113,7 +115,7 @@ class Universities::Index < BrowserAction end end - query = query.where_chong_wen_baos(cwb_query) + query = query.join("left join chong_wen_baos on universities.id = chong_wen_baos.university_id AND chong_wen_baos.user_id = '#{current_user.id}'").where_chong_wen_baos(cwb_query, auto_inner_join: false) range_max, range_min, query = filter_by_column_action(query)