加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – Rails has_many self referential

发布时间:2020-12-17 03:46:36 所属栏目:百科 来源:网络整理
导读:我有一个帐户模型如下(简化): 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,:foreign_key = "
我有一个帐户模型如下(简化):

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,:foreign_key => "master_account_id"
end

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

解决方法

我认为必须反过来说:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读