有很多孩子和有很多父母

发布于 2024-11-08 19:12:25 字数 1762 浏览 2 评论 0原文

我试图找出模型之间的复杂关系。 我有一个名为“概念”的模型,它有两个继承类型,称为“技能”和“职业”。基本上,这意味着每个概念代表一个类别,但当深入到层次树时,概念也可以是一种技能或职业。

我正在通过使用 STI 来解决这个层次结构。因此,我的概念表架构如下所示:

class CreateConcepts < ActiveRecord::Migration
  def self.up
    create_table :concepts do |t|
      t.string :uri, :null => false, :length => 255
      t.string :type, :null => true, :length => 255
      t.integer :isco_code, :null => true
      t.timestamps
    end
  end

  def self.down
    drop_table :concepts
  end
end

类型列确定概念是真正的“概念”还是“技能”/“职业”。 但现在的问题有以下关系:

编辑:

  • 概念可以属于单个父概念
  • 职业可以属于单父概念
  • 一项技能可以属于多个父概念
  • 一项技能没有子项
  • 职业没有子项

所以基本上你会有这样的东西:

>                             concept1
>                  concept2                  concept3
>        concept4       concept5        concept6     concept7    skill1 
> occup1   skill2 occup2    skill5
> occup7    skill2  occup3   skill4
> occup4    skill1 occup8

我希望图片我想解释的内容有点清楚。 目前我已经创建了以下迁移来尝试解决父子关系,但我不确定如何将其与关联映射...

class CreateConceptLinks < ActiveRecord::Migration
  def self.up
    create_table :concept_links do |t|
      t.integer :parent_id, :null => false
      t.integer :child_id, :null => false
      t.timestamps
    end
  end

  def self.down
    drop_table :concept_links
  end
 end

我想要最终得到的是以下可能性:

concepta.parents => a Concept object
conceptb.children => an array of Conept objects
Occupation.parents => a Concept object
Occupation.children => []
Skill.parents => an array of Concept objects
Skill.children => []

希望这是可能的。 ..

I'm trying to figure out a complex relation between a Model.
I have a model called "Concept", which has two inheriting types called "Skill" and "Occupation". Basicly this means that each concept represents a category, but a concept can also be a skill or an occupation when going deep enough into the hierychal tree.

I'm solving this hierachy by using STI. So my schema for the Concepts table looks like this:

class CreateConcepts < ActiveRecord::Migration
  def self.up
    create_table :concepts do |t|
      t.string :uri, :null => false, :length => 255
      t.string :type, :null => true, :length => 255
      t.integer :isco_code, :null => true
      t.timestamps
    end
  end

  def self.down
    drop_table :concepts
  end
end

The type column determins whether the Concept is a real "Concept" or a "Skill"/"Occupation".
The problem now however the following relations:

EDIT:

  • A Concept can belong to a single parent Concept
  • An Occupation can belong to a single parent Concept
  • A Skill can belong to multiple parent Concepts
  • A skill has no children
  • An occupation has no children

so basicly you'd have something like this:

>                             concept1
>                  concept2                  concept3
>        concept4       concept5        concept6     concept7    skill1 
> occup1   skill2 occup2    skill5
> occup7    skill2  occup3   skill4
> occup4    skill1 occup8

I hope the picture is a bit clear what I'm trying to explain.
Currently I have created the following migration to try to solve the parent-child relation but I'm not sure how to map this with the associations...

class CreateConceptLinks < ActiveRecord::Migration
  def self.up
    create_table :concept_links do |t|
      t.integer :parent_id, :null => false
      t.integer :child_id, :null => false
      t.timestamps
    end
  end

  def self.down
    drop_table :concept_links
  end
 end

What I want to end up with is the following posssibilities:

concepta.parents => a Concept object
conceptb.children => an array of Conept objects
Occupation.parents => a Concept object
Occupation.children => []
Skill.parents => an array of Concept objects
Skill.children => []

Hope this is even possible...

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

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

发布评论

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

评论(1

羅雙樹 2024-11-15 19:12:25

您可以在 Rails 中建模层次关系。您的迁移已完成大部分工作。添加下面的关系应该允许您执行您想要的方法调用:

def Concept < ActiveRecord::Base
  has_many :child_links, :class_name => 'ConceptLink', :foreign_key => 'parent_id'
  has_many :children, :through => :child_links

  has_many :parent_links, :class_name => 'ConceptLink', :foreign_key => 'child_id'
  has_many :parents, :through => :parent_links
end

def ConceptLink < ActiveRecord::Base
  belongs_to :child, :class_name => "Concept"
  belongs_to :parent, :class_name => "Concept"
end

我还看一下 这篇博文很好地解释了 Rails 中的父子映射。

You can model hierarchical relations in rails. You've got most of the way there with your migrations. Adding the relations below should allow you to do the method calls you'd like:

def Concept < ActiveRecord::Base
  has_many :child_links, :class_name => 'ConceptLink', :foreign_key => 'parent_id'
  has_many :children, :through => :child_links

  has_many :parent_links, :class_name => 'ConceptLink', :foreign_key => 'child_id'
  has_many :parents, :through => :parent_links
end

def ConceptLink < ActiveRecord::Base
  belongs_to :child, :class_name => "Concept"
  belongs_to :parent, :class_name => "Concept"
end

I'd also take a look at this blog posting which does a very good of explaining parent-child mappings in rails.

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