DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s): [:id, :title]. Non-attribute arguments will be disallowed in Rails 6.1. This method should not be called with user-provided values, such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql(). (called from irb_binding at (irb):35)
為何會出現這個訊息呢? 原來是 Rails 已經在版本 5.2 時, 就將部分的 raw sql 的查詢方式列為有潛在風險.
因此, 先看到內部的 JOIN 語法, Task 與 Tagging 除了 JOIN 之外, 還有設定日期條件以及 GROUP BY 的方式.
1 2 3 4 5 6 7 8 9 10
INNER JOIN ( SELECT "TASKS"."USER_ID", COUNT("TAGGINGS"."TAG_ID") AS TAGGINGS_COUNT FROM "TASKS" INNER JOIN "TAGGINGS" ON "TASKS"."ID" = "TAGGINGS"."TASK_ID" AND "TASKS"."START_DATE" >= '2020-06-25' GROUP BY "TASKS"."USER_ID" ) TOTAL_TAGGINGS
defjoin_clause @join_clause ||= begin users = User.arel_table users.join(total_taggings).on(users[:id].eq(total_taggings[:user_id])).join_sources end end
defjoin_clause @join_clause ||= begin users = User.arel_table users.join(total_taggings).on(users[:id].eq(total_taggings[:user_id])).join_sources end end
deftotal_taggings @total_taggings ||= begin tasks = Task.arel_table taggings = Tagging.arel_table tasks.join(taggings).on(tasks[:id].eq(taggings[:task_id]) .and(tasks[:start_date].gteq(Date.parse(start_date)))) .project(tasks[:user_id], taggings[:tag_id].count.as('taggings_count')) .group(tasks[:user_id]) .as('total_taggings') end end end