使用Rails和Squeel对复杂条件的结果进行排名

时间:2013-03-14 14:32:25

标签: ruby-on-rails squeel

我可能在这里做些蠢事 - 而且我对其他做法持开放态度 - 但我正在尝试根据计算字段订购我的结果集:

Client.select{['clients.*',
               (cast((surname == matching_surname).as int) * 10 +
                cast((given_names == matching_given_names).as int) + 
                cast((date_of_birth == matching_date_of_birth).as int).as(ranking)]}.
       where{(surname =~ matching_surname) | 
             (given_names =~ matching_given_names) | 
             (date_of_birth == matching_date_of_birth)}.
       order{`ranking`.desc}

我的问题是date_of_birth可能是零。这会导致cast((...).as int)调用返回三个不同的值 - 1如果表达式求值为true;如果表达式评估为0,则false;如果基础列值为nil,则为nil

表达式中的nil值会导致整个排名评估为NIL - 这意味着即使我的记录与surnamegiven_names完全匹配,如果date_of_birth列为nil,则记录的rankingnil

我尝试在检查cast的{​​{1}}中使用复杂表达式,但是使用if not nil or the matching_value时出现Squeel异常失败,而ruby在使用|时对其进行评估和||

我还尝试在别名列的顺序中使用谓词:

or

但是会引发order{[`ranking` != nil, `ranking`.desc]} 异常,抱怨列ActiveRecord不存在。

我的绳索结束了......任何想法?

1 个答案:

答案 0 :(得分:0)

经过一段舞蹈之后,我能够使用一系列外部联接来计算ranking,如下所示:

def self.weighted_by_any (client)
  scope = 
    select{[`clients.*`,
            [
             ((cast((`not rank_A.id is null`).as int) * 100) if client[:social_insurance_number].present?),
             ((cast((`not rank_B.id is null`).as int) * 10) if client[:surname].present?),
             ((cast((`not rank_C.id is null`).as int) * 1) if client[:given_names].present?), 
             ((cast((`not rank_D.id is null`).as int) * 1) if client[:date_of_birth].present?)
            ].compact.reduce(:+).as(`ranking`)
          ]}.by_any(client)

  scope = scope.joins{"left join (" + Client.weigh_social_insurance_number(client).to_sql + ") AS rank_A ON rank_A.id = clients.id"} if client[:social_insurance_number].present?
  scope = scope.joins{"left join (" + Client.weigh_surname(client).to_sql + ") AS rank_B on rank_B.id = clients.id"} if client[:surname].present?
  scope = scope.joins{"left join (" + Client.weigh_given_names(client).to_sql + ") AS rank_C on rank_C.id = clients.id"} if client[:given_names].present?
  scope = scope.joins{"left join (" + Client.weigh_date_of_birth(client).to_sql + ") AS rank_D on rank_D.id = clients.id"} if client[:date_of_birth].present?
  scope.order{`ranking`.desc}
end

其中Client.weigh_<attribute>(client)是另一个范围,如下所示:

def self.weigh_social_insurance_number (client)
  select{[:id]}.where{social_insurance_number == client[:social_insurance_number]}
end

这允许我打破nil检查中的值的比较,因此删除了我的布尔计算中的第三个值(TRUE =&gt; 1,FALSE =&gt; 0)。

清洁?高效?优雅?也许不是......但是工作。 :)

编辑基于新信息

由于Bigxiang's answer,我已经将它重构为更美丽的东西。以下是我的想法:

首先,我用sifters替换了weigh_<attribute>(client)范围。我之前发现你可以在范围的select{}部分使用筛子 - 我们将在一分钟内使用它们。

sifter :weigh_social_insurance_number do |token|
  # check if the token is present - we don't want to match on nil, but we want the column in the results
  # cast the comparison of the token to the column to an integer -> nil = nil, true = 1, false = 0
  # use coalesce to replace the nil value with `0` (for no match)
  (token.present? ? coalesce(cast((social_insurance_number == token).as int), `0`) : `0`).as(weight_social_insurance_number)
end

sifter :weigh_surname do |token|
  (token.present? ? coalesce(cast((surname == token).as int), `0`) :`0`).as(weight_surname)
end

sifter :weigh_given_names do |token|
  (token.present? ? coalesce(cast((given_names == token).as int), `0`) : `0`).as(weight_given_names)
end

sifter :weigh_date_of_birth do |token|
  (token.present? ? coalesce(cast((date_of_birth == token).as int), `0`) : `0`).as(weight_date_of_birth)
end

因此,让我们使用筛子来创建一个范围来衡量我们所有的标准:

def self.weigh_criteria (client)
  select{[`*`, 
          sift(weigh_social_insurance_number, client[:social_insurance_number]),
          sift(weigh_surname, client[:surname]),
          sift(weigh_given_names, client[:given_names]),
          sift(weigh_date_of_birth, client[:date_of_birth])
        ]}
end

现在我们可以确定提供的条件是否与列值匹配,我们使用另一个筛选器计算我们的排名:

sifter :ranking do
  (weight_social_insurance_number * 100 + weight_surname * 10 + weight_date_of_birth * 5 + weight_given_names).as(ranking)
end

并将它们全部添加在一起,使我们的范围包含所有模型属性和计算属性:

def self.weighted_by_any (client)
  # check if the date is valid 
  begin 
    client[:date_of_birth] = Date.parse(client[:date_of_birth])
  rescue => e
    client.delete(:date_of_birth)
  end

  select{[`*`, sift(ranking)]}.from("(#{weigh_criteria(client).by_any(client).to_sql}) clients").order{`ranking`.desc}
end

所以,我现在可以搜索一个客户,并根据他们与提供的标准的匹配程度对结果进行排名:

irb(main): Client.weighted_by_any(client)
  Client Load (8.9ms)  SELECT *, 
                              "clients"."weight_social_insurance_number" * 100 + 
                              "clients"."weight_surname" * 10 + 
                              "clients"."weight_date_of_birth" * 5 + 
                              "clients"."weight_given_names" AS ranking 
                       FROM (
                             SELECT *, 
                                    coalesce(cast("clients"."social_insurance_number" = '<sin>' AS int), 0) AS weight_social_insurance_number, 
                                    coalesce(cast("clients"."surname" = '<surname>' AS int), 0) AS weight_surname, 
                                    coalesce(cast("clients"."given_names" = '<given_names>' AS int), 0) AS weight_given_names,         0 AS weight_date_of_birth 
                             FROM "clients" 
                             WHERE ((("clients"."social_insurance_number" = '<sin>' 
                                   OR "clients"."surname" ILIKE '<surname>%') 
                                   OR "clients"."given_names" ILIKE '<given_names>%'))
                            ) clients 
                       ORDER BY ranking DESC

更清洁,更优雅,更好地工作!