mayoneQD mayoneQD - 6 months ago 18
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
else
return user
end
end
end


*relaion model

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


*Action sucess

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


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

Answer

You need to modify this line:

has_many :following_relations, :foreign_key => :follower_id

To:

has_many :following_relations, class_name: 'Relation', :foreign_key => :follower_id