简体   繁体   English

Rails:关系表上的impressionist_count

[英]Rails: impressionist_count on a relationship table

I'm trying to use the impressionist gem to track visits on a relation. 我正在尝试使用印象派瑰宝来跟踪某个关系的访问。 My write method works well: 我的写方法效果很好:

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

I have a has_many, :through relation like so (simplified here but it all works fine): 我有一个has_many, :through关系(在这里简化了,但是一切正常):

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

I'm just running into an issue writing a query to get the relevant impressions based on a component. 我刚遇到编写查询以基于组件获得相关印象的问题。 My failing code presently looks like this: 我失败的代码目前看起来像这样:

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

I get a no method error like so: 我收到这样的无方法错误:

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

I know I'm writing the failing code incorrectly but I'm struggling to find the correct way to write it. 我知道我在错误地编写失败的代码,但是我在努力寻找正确的编写方法。

You need to set-up :counter_cache in your association 您需要在关联中设置:counter_cache

Watch this video, it's clearly explained http://railscasts.com/episodes/23-counter-cache-column 观看此视频,视频已明确说明http://railscasts.com/episodes/23-counter-cache-column

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

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