在查询SELECT "sheets".* FROM "sheets" WHERE ("sheets"."id" > 5)之后生成的Ransack,但是我想在查询SELECT "sheets".* FROM "sheets" WHERE (((score1 + score2)/2) > 5)中实现公式,基本上我想比较公式而不是属性
发布于 2022-01-09 21:32:04
为了实现这一点,您需要实现一个自定义兰萨克。
有点像
class Sheet < ApplicationRecord
ransacker :average_score do |parent|
(parent.table[:score1] + parent.table[:score2]) / 2
end
end然后你可以使用一般的ransack语法。例如average_score_gt或使用更高级的版本(我相信)
Sheet.ransack(
conditions: [{
attributes: ['average_score'],
predicate_name: 'gt',
values: [5]
}]
)发布于 2022-01-09 09:01:57
是的,这可以使用ActiveRecord来完成。我更喜欢从params中获得用户选择的值。
params:
{ filters: [{column: "id", operator: ">", value: 5}] }这样,您就可以在后端构建条件。
condition = ActiveRecord::Base.sanitize_sql('id > 5')然后,您可以将其传递给查询。
Sheet.where(condition)https://stackoverflow.com/questions/70638974
复制相似问题