Rails - 父/子关系

发布于 2024-10-16 04:22:58 字数 175 浏览 6 评论 0原文

我目前正在使用标准的一对一关系来处理父/子关系:

class Category < ActiveRecord::Base
  has_one :category
  belongs_to :category
end

是否有推荐的方法来做到这一点,或者这样可以吗?

I'm currently using a standard one-to-one relationship to handle parent/child relationships:

class Category < ActiveRecord::Base
  has_one :category
  belongs_to :category
end

Is there a recommended way to do it or is this ok?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

≈。彩虹 2024-10-23 04:22:58

您需要调整您使用的名称才能使其正常工作 - 您指定关系的名称,然后告诉 AR 该类是什么:

class Category < ActiveRecord::Base
  has_one :child, :class_name => "Category"
  belongs_to :parent, :class_name => "Category" 
end

You will need to tweak the names you are using to get this working - you specify the name of the relationship, and then tell AR what the class is:

class Category < ActiveRecord::Base
  has_one :child, :class_name => "Category"
  belongs_to :parent, :class_name => "Category" 
end
唯憾梦倾城 2024-10-23 04:22:58

我发现我必须对 @equivalent8 的解决方案进行一些小的更改,以使其适用于 Rails 5 (5.1.4):

class Category < ActiveRecord::Base
  has_many :children, :class_name => "Category", foreign_key: 'parent_id'
  belongs_to :parent, :class_name => "Category", foreign_key: 'parent_id', :optional => true
end

如果没有 foreign_key 声明,Rails 会尝试通过 Organization_id 而不是 Parent_id 查找子项和窒息。

如果没有 :optional => ,Rails 也会卡住。 true 声明了 own_to 关联,因为在 Rails 5 中,belongs_to 要求默认分配一个实例。在这种情况下,您必须分配无限数量的父级。

I found that I had to make a minor change to @equivalent8's solution to make it work for Rails 5 (5.1.4):

class Category < ActiveRecord::Base
  has_many :children, :class_name => "Category", foreign_key: 'parent_id'
  belongs_to :parent, :class_name => "Category", foreign_key: 'parent_id', :optional => true
end

Without the foreign_key declaration, Rails tries to find the children by organization_id instead of parent_id and chokes.

Rails also chokes without the :optional => true declaration on the belongs_to association since belongs_to requires an instance to be assigned by default in Rails 5. In this case, you would have to assign an infinite number of parents.

对你再特殊 2024-10-23 04:22:58

有_许多版本:

class Category < ActiveRecord::Base
  has_many :children, :class_name => "Category"
  belongs_to :parent, :class_name => "Category" 
end

#migratio
class CreateCategories < ActiveRecord::Migration 
 def change
    create_table :categories do |t|
      t.integer :parent_id
      t.string  :title

      t.timestamps null: false
    end
 end
end

# RSpec test 
require 'rails_helper'
RSpec.describe Category do
  describe '#parent & #children' do
    it 'should be able to do parent tree' do
      c1 = Category.new.save!
      c2 = Category.new(parent: c1).save!

      expect(c1.children).to include(c2)
      expect(c2.parent).to eq c1
    end
  end
end

has_many version:

class Category < ActiveRecord::Base
  has_many :children, :class_name => "Category"
  belongs_to :parent, :class_name => "Category" 
end

#migratio
class CreateCategories < ActiveRecord::Migration 
 def change
    create_table :categories do |t|
      t.integer :parent_id
      t.string  :title

      t.timestamps null: false
    end
 end
end

# RSpec test 
require 'rails_helper'
RSpec.describe Category do
  describe '#parent & #children' do
    it 'should be able to do parent tree' do
      c1 = Category.new.save!
      c2 = Category.new(parent: c1).save!

      expect(c1.children).to include(c2)
      expect(c2.parent).to eq c1
    end
  end
end
永言不败 2024-10-23 04:22:58

我想,应该是foreign_key

class Product < ActiveRecord::Base
 has_many :children, :foreign_key => "parent_id" , :class_name => "Product"
 belongs_to :parent, :class_name => "Product" 
end

控制台:

2.4.3 :004 > product  = Product.find_by_name "BOB"

 => #<Product id: 1, name: "BOB", parent_id: nil>
2.4.3 :005 > product.children
D, [2019-11-07T14:56:14.273606 #66632] DEBUG -- :   

Product Load (0.7ms) SELECT "products".* FROM "products" WHERE "products"."parent_id" = $1  [["parent_id", 1]]

 => #<ActiveRecord::Associations::CollectionProxy 
[#<Product id: 7, parent_id: 1>, 
#<Product id: 8,  parent_id: 1>, 
#<Product id: 9, parent_id: 1>]>

I think , should be foreign_key

class Product < ActiveRecord::Base
 has_many :children, :foreign_key => "parent_id" , :class_name => "Product"
 belongs_to :parent, :class_name => "Product" 
end

Console :

2.4.3 :004 > product  = Product.find_by_name "BOB"

 => #<Product id: 1, name: "BOB", parent_id: nil>
2.4.3 :005 > product.children
D, [2019-11-07T14:56:14.273606 #66632] DEBUG -- :   

Product Load (0.7ms) SELECT "products".* FROM "products" WHERE "products"."parent_id" = $1  [["parent_id", 1]]

 => #<ActiveRecord::Associations::CollectionProxy 
[#<Product id: 7, parent_id: 1>, 
#<Product id: 8,  parent_id: 1>, 
#<Product id: 9, parent_id: 1>]>

迟月 2024-10-23 04:22:58

如果您已经有一个模型 类别和一个 类别(在schema.rb中),您可以这样做模型:

class Category < ApplicationRecord
    has_many :children, class_name: 'Category', foreign_key: :parent_id
    belongs_to :parent, class_name: 'Category', optional: true
end

这作为迁移:

class AddParentIdToCategories < ActiveRecord::Migration[6.0]
  def change
    add_column :categories, :parent_id, :integer
  end
end

注意:在较新版本的 Ruby 中,您不需要火箭哈希 =>

If you already have a model Category and a table categories (in schema.rb) you could do this to the model:

class Category < ApplicationRecord
    has_many :children, class_name: 'Category', foreign_key: :parent_id
    belongs_to :parent, class_name: 'Category', optional: true
end

and this as a migration:

class AddParentIdToCategories < ActiveRecord::Migration[6.0]
  def change
    add_column :categories, :parent_id, :integer
  end
end

NOTE: In newer versions of Ruby you don't need the rocket hashes =>

给妤﹃绝世温柔 2024-10-23 04:22:58

由于这种关系是对称的,我实际上发现与托比所写的不同,我更喜欢以下内容:

class Category < ActiveRecord::Base 
  has_one :parent, :class_name => "Category" 
  belongs_to :children, :class_name => "Category"
end

出于某种原因“有一个父母,许多孩子”是我的想法,而不是“有许多父母,只有一个孩子”

Since the relation is symmetric, I actually find that different than what Toby wrote, that I prefer the following:

class Category < ActiveRecord::Base 
  has_one :parent, :class_name => "Category" 
  belongs_to :children, :class_name => "Category"
end

For some reason "has one parent, many children" is the way my mind things, not "has many parents, only one child"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文