mayoneQD mayoneQD - 1 year ago 96
Ruby Question

Ruby on Rails NameError

I'm making a new app for following user and I get some troubles. This is my error:

NameError in Welcome#sucess
Showing ../welcome/sucess.html.erb where line #13 raised:
uninitialized constant User::FollowingRelation
Extracted source (around line #13):

10: <br />
11: Following
12: <ul>
13: <% @user.following.each do |u| %>
14: <li><%= link_to u.username, u %></li>
15: <% end %>
16: </ul>

I can find User::FollowingRelation in my code:

*My user model

class User < ActiveRecord::Base

attr_accessible :pass, :username
# Who am I following?
has_many :following_relations, :foreign_key => :follower_id
has_many :following, :through => :following_relations
# Who am I followed by?
has_many :follower_relations, :class_name => "Relation", :foreign_key => :following_id
has_many :followers, :through => :follower_relations

validates :username, :pass, :presence => true
validates :username, :pass, :length => { :minimum => 4 }
validates :username, :uniqueness => true

def self.login(username,pass)
user = find_by_username(username) and user = find_by_pass(pass)
if user.nil?
return nil
return user

*relaion model

class Relation < ActiveRecord::Base
belongs_to :follower, :class_name => "User"
belongs_to :following, :class_name => "User"

*Action sucess

def sucess
@user = User.find(params[:id])
@relation =

I'm new in Ruby on Rails, so please help me:)

Answer Source

You need to modify this line:

has_many :following_relations, :foreign_key => :follower_id


has_many :following_relations, class_name: 'Relation', :foreign_key => :follower_id
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download