繁体   English   中英

Rails:关系表上的impressionist_count

[英]Rails: impressionist_count on a relationship table

我正在尝试使用印象派瑰宝来跟踪某个关系的访问。 我的写方法效果很好:

@document.publications.each do |publication|
  impressionist(publication)
end

我有一个has_many, :through关系(在这里简化了,但是一切正常):

class Document
  has_many :components, :through => :publications
end

我刚遇到编写查询以基于组件获得相关印象的问题。 我失败的代码目前看起来像这样:

<% document.components.each do |component| %>
  <%= component.publications.where(:document_id => document.id).impressionist_count %>
<% end %>

我收到这样的无方法错误:

undefined method 'impressionist_count' for #<ActiveRecord::Relation:blah

我知道我在错误地编写失败的代码,但是我在努力寻找正确的编写方法。

您需要在关联中设置:counter_cache

观看此视频,视频已明确说明http://railscasts.com/episodes/23-counter-cache-column

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM