public
Created

@user.followed_users.count won't output

  • Download Gist
Error Message
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
ActiveRecord::HasManyThroughSourceAssociationNotFoundError in Static_pages#home
 
Showing C:/Users/Marc/rails_projects/sample_app_2/app/views/shared/_stats.html.erb where line #5 raised:
 
Could not find the source association(s) :followed_id in model Relationship. Try 'has_many :followed_users, :through => :relationships, :source => <name>'. Is it one of :follower or :followed?
Extracted source (around line #5):
 
2: <div class="stats">
3: <a href="<%= following_user_path(@user) %>">
4: <strong id="following" class="stat">
5: <%= @user.followed_users.count %>
6: </strong>
7: following
8: </a>
Trace of template inclusion: app/views/static_pages/home.html.erb
 
Rails.root: C:/Users/Marc/rails_projects/sample_app_2
 
Application Trace | Framework Trace | Full Trace
app/views/shared/_stats.html.erb:5:in `_app_views_shared__stats_html_erb___1037190699_31233168'
app/views/static_pages/home.html.erb:8:in `_app_views_static_pages_home_html_erb___370430801_31242312'
Request
 
Parameters:
 
None
Show session dump
 
Show env dump
 
Response
 
Headers:
 
None
models-relationship.rb
Ruby
1 2 3 4 5 6 7 8 9
class Relationship < ActiveRecord::Base
attr_accessible :followed_id, :follower_id
 
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
 
validates :follower_id, presence: true
validates :followed_id, presence: true
end
models-user.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: "followed_id"
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
 
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensative: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
def feed
Micropost.where("user_id = ?", id)
end
 
def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end
 
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
 
def unfollow!(other_user)
relationship.find_by_followed_id(other_user.id).destroy
end
 
private
 
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
shared-_stats.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<% @user ||= current_user %>
<div class="stats">
<a href="<%= following_user_path(@user) %>">
<strong id="following" class="stat">
<%= @user.followed_users.count %>
</strong>
following
</a>
<a href="<%= followers_user_path(@user) %>">
<strong id="followers" class="stat">
<%= @user.followers.count %>
</strong>
followers
</a>
</div>
static_pages-home.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<% if signed_in? %>
<div class="row">
<aside class="span4">
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<%= render 'shared/stats' %>
</section>
<section>
<%= render 'shared/micropost_form' %>
</section>
</aside>
<div class="span8">
<h3>Micropost Feed</h3>
<%= render 'shared/feed' %>
</div>
</div>
<% else %>
<div class="center hero-unit">
<h1>Welcome to the Sample App</h1>
 
<h2>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</h2>
 
<%= link_to "Sign up now!", signup_path,
class: "btn btn-large btn-primary" %>
</div>
 
<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>
<% end %>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.