我正在使用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')

我该如何解决?

最佳答案

事实证明,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/

关于activerecord - activeadmin和acts_as_taggable_on产生ambiguous_column_name错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41818052/

10-13 08:59