Questions tagged «rails-models»

3
Rails has_one:通过关联
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 可能会让我们做类似的事情: supplier.account_history 否则将达到: supplier.account.history 如果仅是为了更简单的访问,那么从技术上讲,可能存在一对一关联,该关联将一个模型与经过n-1个模型的第n个模型连接起来,以便于访问。除了快捷方式,我还有什么想念的吗?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.