Question

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):

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.

Was it helpful?

Solution

You need to set-up :counter_cache in your association

Watch this video, it's clearly explained http://railscasts.com/episodes/23-counter-cache-column

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top