3

我正在使用active_admin和acts_As_taggable_on,我試圖做一個過濾器。下面是型號代碼:activeadmin和acts_as_taggable_on生成ambiguous_column_name錯誤

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') 

我該如何解決這個問題?

+0

問題是'created_at>'2017-01-17 00:22:53.923894'' 'created_at'存在於'taggings'和'people'上 – TheRealMrCrowley

+0

你可以發佈代碼的調用嗎?似乎缺少某些東西 – TheRealMrCrowley

回答

0

事實證明,active_admin使用ransack gem來處理它的過濾器。我不得不做出定製「ransackers」的模式,使這項工作:

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/