【发布时间】:2012-11-28 17:46:20
【问题描述】:
您好,对于更复杂的模型,何时使用 :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)吗?
【问题讨论】:
标签: ruby-on-rails