Rails has_many self referential

I have an account model as follows (simplified):

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 is currently working correctly, but I also want to have access to @account.child_accounts - what do I need to do to fix this?

+4
source share
1 answer

I think it should be the other way around:

 class Account < ActiveRecord::Base has_many :child_accounts, :class_name => "Account", :foreign_key => "master_account_id" belongs_to :master_account, :class_name => "Account" end 
+9
source

All Articles