在 A Guide to Active Record Associations 中描述了 :include 和 :through. 功能看起来是相同的,下面就是两段分别使用 :include
和 :through
的 model 代码,想知道下这两段在功能上有差别吗?
class Supplier < ActiveRecord::Base
has_one :account, :include => :representative
end
class Account < ActiveRecord::Base
belongs_to :supplier
belongs_to :representative
end
class Representative < ActiveRecord::Base
has_many :accounts
end
class Supplier < ActiveRecord::Base
has_one :account
has_one :representative, :through => :account
end
class Account < ActiveRecord::Base
belongs_to :supplier
belongs_to :representative
end
class Representative < ActiveRecord::Base
has_many :accounts
end