Search code examples
activerecordactiveadminformtasticacts-as-taggable-onacts-as-taggable

activeadmin and acts_as_taggable_on generates ambiguous_column_name error


I am using active_admin and acts_As_taggable_on and I am trying to make a filter. Here is the model code:

class Person < ApplicationRecord
    acts_as_taggable_on :expertise, :industry
end

Here is the filter:

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

and here is the error i get when submitting the filter:

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

How do I fix this?


Solution

  • It turns out active_admin uses the ransack gem to handle it's filters. I had to make custom "ransackers" in the model to make this work:

    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
    

    I got this from the following article and the corrections made in the comments:

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