PROBLEM - undefined method ' -' for "books, children":String
'acts_as_taggable_on_steroids'已经有一段时间没有维护了。它以前工作得很好,直到最近。现在,当我尝试创建或更新标签时,我得到一个错误消息->未定义方法' -' for "books, children":String
--- Codings ----
acts_as_taggable.rb
1. module ActiveRecord #:nodoc:
2. module Acts #:nodoc:
3. module Taggable #:nodoc:
4. def self.included(base)
5. base.extend(ClassMethods)
6. end
7. module ClassMethods
8. def acts_as_taggable
9. has_many :taggings, :as => :taggable, :dependent => :destroy
10. has_many :tags, :through => :taggings
11. before_save :save_cached_tag_list
12. after_create :save_tags
13. after_update :save_tags
14. include ActiveRecord::Acts::Taggable::InstanceMethods
15. extend ActiveRecord::Acts::Taggable::SingletonMethods
16. alias_method_chain :reload, :tag_list
::::::::::::::::::::::::
17. def save_tags
18. return unless @tag_list
19. new_tag_names = @tag_list - tags.map(&:name)
20. old_tags = tags.reject { |tag| @tag_list.include?(tag.name) }
21. self.class.transaction do
22. if old_tags.any?
23. taggings.find(:all, :conditions => ["tag_id IN (?)",
24. old_tags.map(&:id)]).each(&:destroy)
25. taggings.reset
26. end
27. new_tag_names.each do |new_tag_name|
28. tags << Tag.find_or_create_by_name(new_tag_name)
29. end
30. end
31. true
32. end
33. end
注意:第19行导致了这个问题!
tag.rb
1. class Tag < ActiveRecord::Base
2. attr_accessible :name
3. has_many :taggings, :dependent => :destroy
4. validates :name, :presence=>true
5. validates_uniqueness_of :name
6. def self.find_or_create_by_name(name)
7. Tag.where(:name=>name).first_or_create do |tag|
8. tag.name = name
9. end
10. end
任何帮助都会很感激。谢谢你。
@tag_list
在哪里创建?原来是数组,现在是字符串
如果您想要快速修复,您可以将第19行更改为:
new_tag_names = @tag_list.split(', ') - tags.map(&:name)