【问题标题】:How to create Many-to-Many within the same table in rails如何在rails的同一个表中创建多对多
【发布时间】:2018-08-23 21:09:03
【问题描述】:

我只想在 Rails 中创建一个表(域:包含域),其中包含各种字段:ID、名称(它包含域和子域)和 TLD(顶级域)。例如:account.referal.com,在此帐户和推荐人中,将作为单独的值存储在名称中,com 将存储在 TLD 字段中。并且域和子域应该在同一个表中具有多对多的关系,并且应该根据该关系从表中形成一个 URL。

ask.facebook.com: ask => 1 facebook => 1

Domains Table
 ID
 Name
 TLD
Domain Relationship
 Parent.      Child.
 Domain Key.  Domain Key.
 1            1

通过引用 id 和 tld 将在未来创建一个 url。

【问题讨论】:

  • 你有什么问题?

标签: ruby-on-rails ruby associations rails-activerecord


【解决方案1】:

您需要添加另一个表(多对多)来保存域表的关系:

Class DomainChildren
      belongs_to :domain
      belongs_to :domain_child, class_name: 'Domain'
end


Class Domain
     has_many :domain_children     
End

【讨论】:

  • @WuJo 是的,它是一棵树
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-29
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多