使用ActiveRecord在Rails中实现复杂的关系

编程入门 行业动态 更新时间:2024-10-26 21:18:31
本文介绍了使用ActiveRecord在Rails中实现复杂的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

来自另一个问题:

我需要实现的是:用其创建多个类别 自己指定的信息字段(即汽车与 宠物),一旦创建了此类类别,我需要的命令将是 调用后,将为每个类别创建一个新表.我知道我可以 将所有字段存储为某种字符串,然后对其进行处理以显示 正确,但是我需要为我的Web应用提供高级搜索功能, 为每个类别创建单独的表似乎是最好的方法 实现这一目标.我真的很想听听替代方案

What I need to achieve is this: Create multiple categories with their own designated information fields (ie. Cars have different fields from Pets) and once such a category is created, the commands I need will be invoked and a new table for each category will be made. I know I could store all fields as some sort of string and then process it to display it properly, but I need a advanced search function for my web app and creating separate tables for each category seems the best way to achieve it. I would really like to hear alternatives for this

因此,在这种情况下,我需要类别来保存该特定类别所需的所有输入字段.因此,在管理中,我将拥有以下表格:可以在其中添加类别名称,一些其他相关信息,然后在这些字段中收集有关 HTML字段的信息在输入该特定类别时向用户显示.

So I have this situation where I need categories to hold all input fields needed for that certain category. So in administration I'd have this form where I'd be able to add the category name, some other relevant information to the category itself, and then these fields that would collect information on what HTML fields to present to the user when making an entry to this certain category.

例如,这将是狗"类别:

For example this would be a Dog category:

  • 类别名称:狗
  • 已启用类别:1
  • 父类别:宠物
  • 字段:
    • 标题-文本字段(将自动添加到每个类别)
    • 品种-选择字段
    • 年龄-数字字段
    • 颜色-文本字段
    • 价格-数字字段(将自动添加到每个类别)
    • 说明-文本区域字段(将自动添加到每个类别)
    • Category name: Dog
    • Category enabled: 1
    • Category parent: Pets
    • Fields:
      • Title - text field (will be auto added to each category)
      • Breed - select field
      • Age - number field
      • Color - text field
      • Price - number field (will be auto added to each category)
      • Description - text area field (will be auto added to each category)

      因此,现在在此阶段,当用户为狗"类别创建所有这些特定字段时,我无法确定当用户单击提交"按钮以保存该类别时会发生什么情况.我想到了这两种解决方案:

      So now at this stage when the user created all these certain fields for the Dog category, Im having trouble figuring what would happen when the user hits the submit button to save this category. I thought of these two solutions:

      • 为每个新类别创建一个新的模型/表格(阅读上面的链接问题),并将所有HTML字段作为列,并在categories表格上存储一行,其中包含有关该类别的一些基本信息
      • 将所有内容存储在categories表中,并具有fields_json列,该列将存储所有HTML字段信息(我实际上不会存储HTML,但是基本信息是关于字段的内容,然后在控制器)作为JSON字符串.我可以在create上很好地呈现这些字段,但是在update上填充这些字段(也许)会很麻烦,并且使用这种替代方法搜索功能不是很有效.
      • Create a new model/table for each new category (Read linked question above) with all the HTML fields as columns and also store a row on the categories table with some basic info about this category
      • Store everything in the categories table and have a fields_json column which will store all HTML field information (I wont be actually storing HTML, but basic info what the fields are about then create the HTML form fields dynamically in a controller) as a JSON string. I would be able to present the fields nicely on create, but on update it would be a hassle to populate those fields (maybe) and a search function would not be very efficient with this alternative.

      所以我要寻找的是第三种选择,这样我可以解决我的问题.什么是解决此问题的有效方法,并且能够使类别具有不同的输入字段,并且能够高效地对这些类别执行搜索?

      So what I'm looking for is a third alternative so I can fix my problem. What would be an efficient way to solve this problem and be able to have categories with different input fields and also be able to efficiently perform searches on these categories?

      我正在处理的项目是在Ruby on Rails 4中创建的,而数据库是在MySQL中创建的.

      The project I'm working on is being created in Ruby on Rails 4 and the database is in MySQL.

      推荐答案

      对于给定的情况,我会:

      For given scenario I would:

    • 创建表类别以存储每个类别
    • 创建表 category_fields 来存储每个类别字段
    • 创建表 collected_categories ,以将来自类别字段的所有收集的数据存储在序列化哈希中
    • create table categories to store each category
    • create table category_fields to store each category field
    • create table collected_categories to store all collected data from category fields in serialized hash
    • 所收集的数据可以轻松地(反)序列化为text列(无论您将使用哪种db引擎).

      Collected data can be easily (de)serialized into text column (no matter of db engine you will use).

      检查那些利用您的问题的资源:动态表单

      Check those sources which utilize your problem: dynamic forms

更多推荐

使用ActiveRecord在Rails中实现复杂的关系

本文发布于:2023-10-22 08:11:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1516953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   ActiveRecord   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!