Repository: mbleigh/acts-as-taggable-on

Issue #863: NoMethodError: undefined method `tagged_with' for … although other methods work fine
Created: 2017-10-03T16:43:41Z
We are using acts-as-taggable-on (version 3.5.0 with Rails 4.1)
strangely, tag_list.add and tag_list.remove work fine, but when I call tagged_with on the same object, I get NoMethodError
[8] pry(main)> u.tagged_with("xyz")
NoMethodError: undefined method `tagged_with' for #<Spree::User:0x007ff5632caed8>
from /Users/jason/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.14.2/lib/active_model/attribute_methods.rb:435:in `method_missing'

my inelegant work-around, which works but is ugly is:
[9] pry(main)> u.tags.collect(&:name).include?("xyz")
=> true

This is a Spree application, which itself has a bunch of gems interoperating. I mention this in case it is relevant.
Is tagged_with implemented via a method_missing?.
The reason I ask is that I suspect maybe there might be other competing code that has already overloaded method_missing in my app, which is the only explanation I can think of as to why this would be like this
(Incidentally, I find the overuse of method missing to be a really bad pattern, as I often have to deal with conflicting gems trying to overload method_missing)

Duplicate issue #868: tagged_with doesn't work with arrays
Created: 2017-10-30T05:18:47Z
I am seeing a problem which produces the following error:
 Refinery::Blog::Post list tagged posts when there are tagged blog posts should have one tagged post
     Failure/Error: @posts = Post.live.newest_first.uniq.tagged_with(@tag_name).page(params[:page])
     
     NoMethodError:
       undefined method `tagged_with' for #<Array:0x007f9dcb342ea0>

The code is using  uniq to remove duplicates from the result before filtering on the tag, but this means that tagged_with is sent an array instead of an ActiveRecord_Relation.
To reproduce:
# script derived from http://www.jonathanleighton.com/articles/2011/awesome-active-record-bug-reports/

gem 'rails', '~>5.1'
gem 'acts-as-taggable-on'

require 'active_record'
require 'acts-as-taggable-on'

ActiveRecord::Base.establish_connection(
  :adapter => 'sqlite3',
  :database => ':memory:'
)

# Create the minimal database schema necessary to reproduce the bug
ActiveRecord::Schema.define do
  create_table :things, :force => true do |t|
  end

  create_table :tags do |t|
    t.string :name
    t.integer :taggings_count, default: 0
  end

  create_table :taggings do |t|
    t.references :tag
    t.references :taggable, polymorphic: true
    t.references :tagger, polymorphic: true
    t.string :context, limit: 128
    t.datetime :created_at
  end
end

# Create the minimal set of models to reproduce the bug
class Thing < ActiveRecord::Base
  acts_as_taggable
end


# Create some test data

thing = Thing.create!
thing.tag_list.add('awesome')
thing.save!

relation = Thing.all
records = Thing.all.uniq

puts "#{relation.class} has #{relation.tagged_with('awesome').count} records"
puts "#{records.class} has #{records.tagged_with('awesome').count} records"

I get the result:
Thing::ActiveRecord_Relation has 1 records
aato-test.rb:49:in `<main>': undefined method `tagged_with' for [#<Thing id: 1>]:Array (NoMethodError)

Closing comment (2017-10-30T05:33:31Z): Perhaps related to #863