0
如何使用关联访问另一个班级。假设我有以下代码。我怎样才能从供应商类别获得账户和账户历史记录,以及如何获得其他两个类别的价值。如何使用关联访问另一个班级
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
我是否能访问account_history直接从供应商还是必须通过账户来访问它像Supplier.find(“那ID”)。account.account_history – asdfkjasdfjk
您可以直接访问它。否则没有意义定义has_one:account_history,通过::account。当你直接访问它时会通过帐户自动查找account_history –