在rails上使用has_many和在ruby中使用数组有什么区别? (MongoDB的)

时间:2023-01-07 02:40:42

I want to implement a few simple models to my ruby on rails project. I have the model hierarchy, Topic and Project. A Topic will contain many Projects. I wanted to know what the difference was between adding a "has_many projects" relation in the Topic model vs just adding an array field in the Topic model that stores all of the projects within it.

我想在我的ruby on rails项目上实现一些简单的模型。我有模型层次结构,主题和项目。主题将包含许多项目。我想知道在Topic模型中添加“has_many项目”关系与在Topic模型中添加一个存储其中所有项目的数组字段之间的区别。

I am using Ruby on Rails with mongodb as my database and mongoid as the object document mapper.

我使用Ruby on Rails,mongodb作为我的数据库,mongoid作为对象文档映射器。

2 个解决方案

#1


0  

Let's say in you're Topic model:

让我们说你是主题模型:

has_many :projects

Now, if you query like, @project.topic will execute a single query.

现在,如果您查询,@ project.topic将执行单个查询。

Let's say, you add a column in topic which stores project_ids, e.g.: [1,2,3] Now if you want to find any individual @topic projects you need to do like this:

假设您在topic中添加了一个存储project_ids的列,例如:[1,2,3]现在,如果您想要找到任何单独的@topic项目,您需要这样做:

  • first, fetch the @topic.project_ids
  • 首先,获取@ topic.project_ids

  • Then, for each id query to the Project table, if array contains 3 values then 3 query will run.
  • 然后,对于Project表的每个id查询,如果数组包含3个值,则将运行3个查询。

So, all is about execution time.

所以,一切都与执行时间有关。

#2


0  

Array Store

you can use to take reference ids of child association. eg

你可以用来参考儿童协会的参考ID。例如

 users: 
  [
   { 
     _id: ObjectId('oii3213ui23uo')
     name: 'John',
     email: 'jhn@example.com',
     project_ids: ['oii3213ui23u1', 'oii3213ui23uo2' ,'oii3213ui23uo3']
   }
  ]
 projects:
  [
   {
     _id: ObjectId('oii3213ui23u1'), 
     title: 'Project 1' 
   },
   {
     _id: ObjectId('oii3213ui23u2'), 
     title: 'Project 2' 
   },
   {
     _id: ObjectId('oii3213ui23u3'), 
     title: 'Project 3' 
   }

  ] 

has_many

You can use to embed child associations in parent document. eg

您可以使用在父文档中嵌入子关联。例如

users:
 [
  { 
     _id: ObjectId('oii3213ui23uo')
     name: 'John',
     email: 'jhn@example.com',
     projects: [ 
      {            
        title: 'Project 1' 
      },
      {           
       title: 'Project 2' 
      },
      {           
        title: 'Project 3' 
      }
     ]
   }
 ]

#1


0  

Let's say in you're Topic model:

让我们说你是主题模型:

has_many :projects

Now, if you query like, @project.topic will execute a single query.

现在,如果您查询,@ project.topic将执行单个查询。

Let's say, you add a column in topic which stores project_ids, e.g.: [1,2,3] Now if you want to find any individual @topic projects you need to do like this:

假设您在topic中添加了一个存储project_ids的列,例如:[1,2,3]现在,如果您想要找到任何单独的@topic项目,您需要这样做:

  • first, fetch the @topic.project_ids
  • 首先,获取@ topic.project_ids

  • Then, for each id query to the Project table, if array contains 3 values then 3 query will run.
  • 然后,对于Project表的每个id查询,如果数组包含3个值,则将运行3个查询。

So, all is about execution time.

所以,一切都与执行时间有关。

#2


0  

Array Store

you can use to take reference ids of child association. eg

你可以用来参考儿童协会的参考ID。例如

 users: 
  [
   { 
     _id: ObjectId('oii3213ui23uo')
     name: 'John',
     email: 'jhn@example.com',
     project_ids: ['oii3213ui23u1', 'oii3213ui23uo2' ,'oii3213ui23uo3']
   }
  ]
 projects:
  [
   {
     _id: ObjectId('oii3213ui23u1'), 
     title: 'Project 1' 
   },
   {
     _id: ObjectId('oii3213ui23u2'), 
     title: 'Project 2' 
   },
   {
     _id: ObjectId('oii3213ui23u3'), 
     title: 'Project 3' 
   }

  ] 

has_many

You can use to embed child associations in parent document. eg

您可以使用在父文档中嵌入子关联。例如

users:
 [
  { 
     _id: ObjectId('oii3213ui23uo')
     name: 'John',
     email: 'jhn@example.com',
     projects: [ 
      {            
        title: 'Project 1' 
      },
      {           
       title: 'Project 2' 
      },
      {           
        title: 'Project 3' 
      }
     ]
   }
 ]