Created
July 8, 2012 06:29
-
-
Save sourcec0de/3069665 to your computer and use it in GitHub Desktop.
Impressionist belongs_to through has_many for Users association
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post < ActiveRecord::Base | |
belongs_to :user | |
#...Other model attributes | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_many :posts | |
#...Other model attributes | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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