acts_as_taggable_on: NoMethodError: undefined method `add'

acts_as_taggable_on: NoMethodError: undefined method `add'

我正在尝试在我的 Rails 项目中使用 acts_as_taggable gem,我不知道是否有人可以指出我做错了什么,或者是否项目中有错误。

我已经提交了项目问题:https://github.com/mbleigh/acts-as-taggable-on/issues/688 虽然还没有人回应。

我无法使用添加标签的基本功能,例如:taglist.add("a")taglist.remove("a) 或使用 taglist = 'a' 设置标签等。这些方法似乎不存在。

我正在使用 Ruby 2.2.3 和 Rails 4.2.3。我的 gem 文件有:

gem 'acts-as-taggable-on', '~> 3.4' 我也试过

gem 'acts-as-taggable-on', '~> 3.5' 在我的模型中,我有:

class Space < ActiveRecord::Base
  acts_as_taggable_on :skills

从 rails 控制台我可以执行以下操作:

> s = Space.first
 => #<Space id: 1172, created_at: "2015-09-24 08:13:09", ...<many fields>... updated_at: "2015-09-24 08:13:09"> 

> s.skills
  ActsAsTaggableOn::Tag Load (2.0ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" =  AND "taggings"."taggable_type" =  AND "taggings"."context" =   [["taggable_id", 1172], ["taggable_type", "Space"], ["context", "skills"]]

=>#

> s.skills.add("a")
NoMethodError: undefined method `add' for #     <ActiveRecord::Associations::CollectionProxy []>
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/activerecord-4.2.3/lib/active_record/relation/delegation.rb:136:in `method_missing'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/activerecord-4.2.3/lib/active_record/relation/delegation.rb:99:in `method_missing'
from (irb):3
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
from /Users/n/code/pop/poprails/bin/rails:8:in `<top (required)>'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
from /Users/n/.rvm/gems/ruby-2.2.3@poprails/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/n/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/n/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'2.2.3 :004

尝试

s.skills.where(name: "a").first_or_create

s.skill_list.add("a")

应该是

> s = Space.first
 => #<Space id: 1172, created_at: "2015-09-24 08:13:09", ...<many fields>... updated_at: "2015-09-24 08:13:09"> 

> s.skill_list.add("a")

或逗号

> s.skill_list.add("a,b,n,m)

然后

s.skill_list

它将 return 数组

  ["a","b","n","m"]