gpt4 book ai didi

mysql - Rails 对来自两列的值进行排序

转载 作者:行者123 更新时间:2023-11-29 13:59:09 24 4
gpt4 key购买 nike

我有两个表,A 和 B。A 和 B 有外键。都是 ActiveRecord。

class A
has_one :b
def valueRendered
b.value1 || b.value2
end
end

class B
( belongs_to :a, :foreign_key => :keyb ) if self.b
end

这里,当 value1 为 null 时,来自 A 中的 valueRendered 方法的值具有 B 的字段 value2 中的值。我必须使用 valueRendered 对表进行排序,即表应根据 value1 和 value2 的值进行排序。如何进行?

Edit: To be more specific, suppose there is one calculated column (a method in rails) which has value from field "value1" but if value1 is null, the value comes from "value2". I want to sort the whole table (not limited to any specific no of records or scope) with respect to this calculated field (method). I am confused how to proceed.

更多编辑:实际示例代码

In the controller,

  sort_fields = ['offer_orders.id','offers.name','offer_orders.created_at',  
'offer_orders.country', 'offer_orders.ip_address',
'fraud_score_daily.adjusted_fraud_probability, fraud_score_daily.fraud_probability',
'players.email','players.account_status',
nil,
nil,
nil,
nil
]

arel = OfferOrder.includes(:offer, :player, :fraud_score_daily).group('fraud_score_daily.offer_order_id').order('dt desc')
arel = paginate_table_data(arel, :sort_fields => sort_fields)

paginate_table_data 方法位于助手中

def paginate_table_data(arel, opts={})
opts.assert_keys(:required => [:sort_fields])
@page_size = page_size
@current_page = current_page
arel.paginate(:page => @current_page, :per_page => params[:iDisplayLength]).
order(order_by(opts[:sort_fields]).join(', '))
end

最佳答案

怎么样:

scope :order_by_value_rendered, joins(:b).order("value1, value2")

关于mysql - Rails 对来自两列的值进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15358164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com