Rails有一个has_one :through关联,通过第二个模型帮助建立与第三个模型的一对一关联.除了建立一个快捷方式关联之外,它的实际用途是什么,否则这将是一个额外的步骤.
以Rails 指南为例:
class Supplier < ActiveRecord::Base
has_one :account
has_one :account_history, :through => :account
end
class Account < ActiveRecord::Base
belongs_to :supplier
has_one :account_history
end
class AccountHistory < ActiveRecord::Base
belongs_to :account
end
Run Code Online (Sandbox Code Playgroud)
可能允许我们做类似的事情:
supplier.account_history
Run Code Online (Sandbox Code Playgroud)
否则将达到:
supplier.account.history
Run Code Online (Sandbox Code Playgroud)
如果它只是为了更简单的访问,那么从技术上讲,可能存在一对一的关联,它将模型与通过n-1模型的某个第n个模型连接起来,以便于访问.除了快捷方式之外,还有什么我想念的吗?