以下のコードはエラーなしで動作します:
= form_for @blog.comments.build, :remote => true do |f|
ただし、以下は「uninitialized constant User::relationship」というエラーになります。
= form_for @blog.user.followers.build do |f|
ユーザー モデルは次のように宣言されます。
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
最初の例は機能するのに、2 番目の例では機能しないのはなぜですか?
編集:ブログモデル:
class Blog < ActiveRecord::Base
belongs_to :user
has_many :comments
end
関係モデル:
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