Tag: ransack

Ransack,Postgres – 从关联表中对列进行排序,使用distinct:true

我有一个使用Ransackgem的应用程序,我将它从Mysql转换为Postgres。 在排序列来自关联表并且distinct选项设置为true的实例中,Postgres会抛出此错误: PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list Ransack github页面说,在这种情况下,“你是独立的。” 什么是最好的 – 任何! – 处理这种情况的策略? q = Contact.includes(:contact_type).search q.sorts = [‘contact_type_name asc’] q.result(distinct: true) PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list 谢谢!

Ransack:使用年龄而不是出生日期

我想使用ransack为用户构建一个高级搜索function。 我有一个小方法来计算从出生日期开始的年龄: def age(dob) now = Time.now.utc.to_date now.year – dob.year – ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1) end 这适用于正常显示( as in age(@user.date_of_birth) ) 但是当使用search_form_for时我不能这样做: 我的问题是:如何在搜索中使用年龄而不是出生日期?