首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检测和解决counter_culture gem的多态关联?

如何检测和解决counter_culture gem的多态关联?
EN

Stack Overflow用户
提问于 2013-07-19 14:42:11
回答 1查看 680关注 0票数 0

我遵循关于多态性关联的RailsCast #154号设置了一个迁移,如下所示:

代码语言:javascript
复制
# db/migrate/20130719120000_create_comments.rb
class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.string :subject
      t.text :message
      t.belongs_to :commentable, polymorphic: true    
      t.timestamps
    end
    add_index :comments, [:commentable_id, :commentable_type]
  end
end

然后,模型如下所示:

代码语言:javascript
复制
# app/models/comment.rb
class Comment < ActiveRecord::Base
  attr_accessible :message, :subject
  belongs_to :commentable, polymorphic: true
  counter_culture :commentable # This does not work, read below.
end

可以将注释设置为多个模型,以下是一个示例:

代码语言:javascript
复制
# app/models/product.rb
class Product < ActiveRecord::Base    
  has_many :comments, as: :commentable
  attr_accessible :name
end

这个很好用。

现在,我还使用了文化 (正如您在注释模型中看到的那样),它简单地计算注释的数量。它将计数值存储为关联模型的属性,在本例中为Product。集成在我将注释模型转换为多态性之前就已经开始工作了。

当我将counter_culture :commentable更改为counter_culture :product时,最后会收到以下错误消息:

代码语言:javascript
复制
NoMethodError: undefined method `product' for #<Comment:0x0000000531cdb0>
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:407:in `method_missing'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/activerecord-3.2.13/lib/active_record/attribute_methods.rb:149:in `method_missing'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/counter_culture-0.1.12/lib/counter_culture.rb:301:in `foreign_key_value'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/counter_culture-0.1.12/lib/counter_culture.rb:243:in `change_counter_cache'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/counter_culture-0.1.12/lib/counter_culture.rb:196:in `block (2 levels) in _update_counts_after_create'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/counter_culture-0.1.12/lib/counter_culture.rb:194:in `each'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/counter_culture-0.1.12/lib/counter_culture.rb:194:in `block in _update_counts_after_create'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/counter_culture-0.1.12/lib/counter_culture.rb:186:in `call'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/counter_culture-0.1.12/lib/counter_culture.rb:186:in `_wrap_in_counter_culture_active'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/counter_culture-0.1.12/lib/counter_culture.rb:193:in `_update_counts_after_create'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `_run__2202164684578441182__create__891150410440608527__callbacks'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_create_callbacks'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/activerecord-3.2.13/lib/active_record/callbacks.rb:268:in `create'
    from /home/jjd/.rvm/gems/ruby-1.9.3-p448@demoapp/gems/activerecord-3.2.13/lib/active_record/persistence.rb:348:in `create_or_update'

这基本上是指_update_counts_after_create中的第192行

代码语言:javascript
复制
# Source: https://github.com/magnusvk/counter_culture/blob/master/lib/counter_culture.rb#L192
# called by after_create callback
def _update_counts_after_create
  _wrap_in_counter_culture_active do
    self.class.after_commit_counter_cache.each do |hash|
      # increment counter cache
      change_counter_cache(hash.merge(:increment => true))
    end
  end
end

当我在设置hash时检查counter_culture :commentable时,可以看到必须将:relation解析为产品模型。

代码语言:javascript
复制
{:relation=>[:commentable], :counter_cache_name=>"comments_count", :column_names=>nil, :foreign_key_values=>nil}

我假设这应该在分配关系的第29行中完成。

代码语言:javascript
复制
:relation => relation.is_a?(Enumerable) ? relation : [relation],

如何测试relation是否是解决产品模型的多态关联?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-08 08:19:07

我发现实际上有一个关于多态性支持的探讨在运行,还有丹·马格拉桑的“叉子”(2013年1月16日最后一次更新,第0.1.8节)。我查过了,它为我打开了盒子。

我要求作者重新建立分支部门的基础,以更新项目。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17748945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档