2

The below code works without error:

  = form_for @blog.comments.build, :remote => true do |f|

However the below results in the error "uninitialized constant User::relationship":

  = form_for @blog.user.followers.build do |f|

User model is declared as below:

class User < ActiveRecord::Base
  has_many :blogs
  has_many :comments

  has_many :relationships, :foreign_key => "follower_id", :dependent => :destroy
  has_many :reverse_relationships, :foreign_key => "followed_id",
                                   :class_name  => "relationship",
                                   :dependent   => :destroy

  has_many :following, :through => :relationships, :source => :followed
  has_many :followers, :through => :reverse_relationships, :source => :follower
end

Why does the first example work but not hte second?

EDIT: Blog model:

class Blog < ActiveRecord::Base
  belongs_to :user
  has_many :comments

end

Relationship model:

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id

  belongs_to :follower, :class_name => "User"
  belongs_to :followed, :class_name => "User"

  validates :follower_id, :presence => true
  validates :followed_id, :presence => true

  validate :validate_followers

  def validate_followers
    errors.add(:follower_id, "You cannot follow yourself") if follower_id == followed_id
  end
end
Jonathan Maddison
  • 1,067
  • 2
  • 11
  • 24

1 Answers1

4

If you change the :class_name option on reverse relationships to:

:class_name => 'Relationship' 

do you still get the problem? It should be the correct case for a class name I believe.

Shadwell
  • 34,314
  • 14
  • 94
  • 99
  • 1
    In my case it was writing `:class_name => 'Relationships'` instead of `:class_name => 'Relationship'` – jmarceli Nov 09 '14 at 20:30