Skip to content

Instantly share code, notes, and snippets.

@sourcec0de
Created July 8, 2012 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sourcec0de/3069665 to your computer and use it in GitHub Desktop.
Save sourcec0de/3069665 to your computer and use it in GitHub Desktop.
Impressionist belongs_to through has_many for Users association
# You can use What ever type of View you want ERB, SLIM, HAML
#
# Once you have your list of ActiveRecord objects inside of @impressions
# instance variable from your controller, run the variable through a
# for loop to get the count for each ActiveRecord object
#
# Optionally, you can create new instance variables and add to their
# values when running the loop so you can get total number of views for
# that specific user.
- @UnViews = 0
- @TotalViews = 0
- for impression in @impressions do
- @TotalViews = @TotalViews + impression.impressionist_count
- @UnViews = @UnViews + impression.impressionist_count(:filter=>:session_hash)
.row-fluid
.span3
h1 All Views
br
h3 = @TotalViews
.span3
h1 Total Unique Views
br
h3 = @UnViews
.row-fluid
.span12
table class="table table-condensed"
thead
tr
th Post Title
th Total Views
th Unique Views
tbody
- for impression in @impressions do
tr
td = link_to impression.title, impression
td = impression.impressionist_count
td = impression.impressionist_count(:filter=>:session_hash)
class Post < ActiveRecord::Base
belongs_to :user
#...Other model attributes
end
class User < ActiveRecord::Base
has_many :posts
#...Other model attributes
end
class UsersController < ActionController::Base
def action
@impressions = current_user.posts.all
# This Pulls all records of ActiveRecord
# posts for current user and assigns it
# an instance variable of @impressions
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment