嗨,我在概念化何时使用:source
以及何时使用:class
更复杂的模型时遇到了麻烦。
这里我有一个有朋友的用户的例子。
class User < ActiveRecord::Base
...
has_many :friendships, :dependent => :destroy
has_many :friends, :through => :friendships, :conditions => "status = 'accepted'"
has_many :requested_friends, :through => :friendships, :source => :friend, :conditions => "status = 'requested'", :order => :created_at
has_many :pending_friends, :through => :friendships, :source => :friend, :conditions => "status = 'pending'", :order => :created_at
end
class Friendship < ActiveRecord::Base
attr_accessible :friend_id, :user_id, :status
belongs_to :user
belongs_to :friend, :class_name => "User", :foreign_key => 'friend_id'
end
有人可以解释为什么友谊是:class_name
而不是:source
吗?这是因为这只是配对(has_many + :source ,belongs_to + :class_name)吗?