3

我正在使用 active_admin 和acts_As_taggable_on,并且正在尝试制作过滤器。这是模型代码:

class Person < ApplicationRecord
    acts_as_taggable_on :expertise, :industry
end

这是过滤器:

filter :industry, as: :select, collection: Person.industry_counts.pluck(:name, :name)

这是我提交过滤器时遇到的错误:

SQLite3::SQLException: ambiguous column name: created_at: SELECT COUNT(DISTINCT "people"."id") FROM "people" LEFT OUTER JOIN "taggings" ON "taggings"."taggable_id" = "people"."id" AND "taggings"."context" = ? AND "taggings"."taggable_type" = ? WHERE "taggings"."tag_id" = 0 AND (created_at > '2017-01-17 00:22:53.923894')

我该如何解决?

4

1 回答 1

0

事实证明 active_admin 使用 ransack gem 来处理它的过滤器。我必须在模型中制作自定义“洗劫者”才能完成这项工作:

def self.in_industry(industries)
  Person.tagged_with(industries, :any => true, :on => :industry).select{|a| a} #use select to convert relation to array
end

ransacker :by_industry, formatter: proc{ |v|
  data = Person.in_industry(v).map(&:id)
  data = data.present? ? data : nil
} do |parent|
  parent.table[:id]
end

我从以下文章和评论中所做的更正中得到了这个:

http://nikhgupta.com/code/activeadmin/custom-filters-using-ransacker-in-activeadmin-interfaces/

于 2017-02-06T23:12:37.770 回答