4

我有一个如下的帐户模型(简化):

class Account < ActiveRecord::Base
    attr_accessible :account_number, :display_name, :master_account_id

    has_many :child_accounts, :class_name => "Account", :foreign_key => "id"
    belongs_to :master_account, :class_name => "Account", :foreign_key => "master_account_id"
end

@account.master_account目前工作正常,但我也希望能够访问@account.child_accounts- 我需要做什么才能解决这个问题?

4

1 回答 1

9

我认为它必须是相反的方式:

class Account < ActiveRecord::Base
  has_many :child_accounts, :class_name => "Account", :foreign_key => "master_account_id"
  belongs_to :master_account, :class_name => "Account"
end
于 2012-06-19T11:39:41.393 回答