Search code examples
ruby-on-railsransack

Ransack on Gutentag tags- undefined method `type' for nil:NilClass


Has anybody gotten Ransack to search Gutentag tag_names ? I have had issues making ActsAsTaggableOn work but Gutentag works like a charm, I just need to be able to search on it..

resource.tag_names returns a list of tags for resource.

I have in my model

  def self.ransackable_attributes(auth_object = nil)
      ['title','content', 'tag_names']
  end

the line erroring is @resources = @q.result(distinct: true).paginate(:page => params[:page])

I'm assuming that tag_names is not returning the right data for Ransack. Any ideas?


Solution

  • This happens because there is no column tag_names in your table. Thus you need to define ransackable_attributes for gutentag_tags table, because that is the table whose name column is called under the hood. Create an initializer for gutentag in config/initializers directory, name it however you like. In that initializer put the following:

    if ActiveRecord::Base.connection.table_exists? 'gutentag_tags'
      model =  Object.const_get "Gutentag::Tag"
      model.singleton_class.class_eval do
        define_method(:ransackable_attributes) do |auth_object|
          auth_object = nil
          ['name']
        end
      end
    end
    

    This should do the trick although I haven't tried it myself. Feel free to ask anything in case you meet problems with this piece of code.