猴子修补和回调错误
Monkey patching and callback error
我在 Rails 4 应用程序中使用了两个 gem:acts_as_tenant and simple_hashtags。
如果一个租户上存在主题标签,则不会为另一个租户重新保存。所以我想覆盖 find_or_create_by_name 和 find_by_name 方法。
为此,我还需要重写 parsed_hashtags method,但要让我的应用使用它,我还需要包含回调
before_save :update_hashtags
我有一个 initializer,我首先使用它来将多租户系统用于主题标签(因此 tenant_id 会自动保存)。我添加了方法,但是在尝试覆盖回调时,我碰壁了。
如果我像 gist 中那样使用扩展 ActiveSupport::Concern,我会收到此错误并且无法启动我的应用程序。
lib/active_support/concern.rb:126:in `included': Cannot define multiple 'included' blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks)
from /Users/schatteleyn/subarashi/config/initializers/hashtags.rb:16:in `<module:Hashtaggable>'
from /Users/schatteleyn/subarashi/config/initializers/hashtags.rb:14:in `<module:SimpleHashtag>'
from /Users/schatteleyn/subarashi/config/initializers/hashtags.rb:1:in `<top (required)>'
如果我使用
的版本
def self.included(base)
base.class_eval do
before_save :do_something
end
end
我收到此错误,可以启动我的应用程序,但在任何页面上都收到错误。
undefined method `before_save' for HashtagConcern:Module
我很茫然,这是我唯一能找到的两个解决方案,而且我似乎无法使它们起作用。
还有其他方法可以在模块中使用回调吗?或者也许另一种方法可以解决按姓名和租户查找问题?
为了让 simple_hashtag 成为租户感知的,只需覆盖 Hashtag 模型的验证,例如:
SimpleHashtag::Hashtag.clear_validators!
module SimpleHashtag
class Hashtag < ActiveRecord::Base
acts_as_tenant :tenant
validates :name, :uniqueness => { :scope => :tenant }
end
end
我在 Rails 4 应用程序中使用了两个 gem:acts_as_tenant and simple_hashtags。
如果一个租户上存在主题标签,则不会为另一个租户重新保存。所以我想覆盖 find_or_create_by_name 和 find_by_name 方法。
为此,我还需要重写 parsed_hashtags method,但要让我的应用使用它,我还需要包含回调
before_save :update_hashtags
我有一个 initializer,我首先使用它来将多租户系统用于主题标签(因此 tenant_id 会自动保存)。我添加了方法,但是在尝试覆盖回调时,我碰壁了。
如果我像 gist 中那样使用扩展 ActiveSupport::Concern,我会收到此错误并且无法启动我的应用程序。
lib/active_support/concern.rb:126:in `included': Cannot define multiple 'included' blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks)
from /Users/schatteleyn/subarashi/config/initializers/hashtags.rb:16:in `<module:Hashtaggable>'
from /Users/schatteleyn/subarashi/config/initializers/hashtags.rb:14:in `<module:SimpleHashtag>'
from /Users/schatteleyn/subarashi/config/initializers/hashtags.rb:1:in `<top (required)>'
如果我使用
的版本def self.included(base)
base.class_eval do
before_save :do_something
end
end
我收到此错误,可以启动我的应用程序,但在任何页面上都收到错误。
undefined method `before_save' for HashtagConcern:Module
我很茫然,这是我唯一能找到的两个解决方案,而且我似乎无法使它们起作用。 还有其他方法可以在模块中使用回调吗?或者也许另一种方法可以解决按姓名和租户查找问题?
为了让 simple_hashtag 成为租户感知的,只需覆盖 Hashtag 模型的验证,例如:
SimpleHashtag::Hashtag.clear_validators!
module SimpleHashtag
class Hashtag < ActiveRecord::Base
acts_as_tenant :tenant
validates :name, :uniqueness => { :scope => :tenant }
end
end