Using ActiveRecord to achieve complex relationships in Rails

From another question :

I need to achieve this: create several categories with their own information fields (i.e. cars have different fields from Pets), and as soon as such a category is created, the commands that I need will be called, and a new table will be created for each category. I know that I can save all the fields as some string, and then process it to display it correctly, but I need an advanced search function for my web application and creating separate tables for each category seems to be the best way to achieve this. I would love to hear alternatives for this.

So, I have a situation where I need categories to store all the input fields needed for this particular category. Therefore, in administration, I would have such a form where I could add the category name, other relevant information for the category itself, and then these fields, which collected information about which HTML fields the user was present when making an entry in this specific category.

For example, this would be the Dog category:

  • Category Name: Dog
  • Category Included: 1
  • Parent Category: Pets
  • Fields:
    • Title - text field (will be added automatically to each category)
    • Breed - select a field
    • Field of Age - Number
    • Color - text box
    • Price field - number (will be added automatically to each category)
    • Description - text field field (will be automatically added to each category)

So, now, at this point, when the user created all these specific fields for the Dog category, I had trouble figuring out what would happen when the user clicks the submit button to save this category. I was thinking about these two solutions:

  • Create a new model / table for each new category (read the related question above) with all HTML fields as columns, and also save a row in the categories table with some basic information about this category
  • Store everything in the categories table and have a fields_json column that will store all the information about the HTML field (I will not actually store HTML, but the basic information about what the fields are, then it dynamically creates the fields of the HTML form in the controller) as a JSON string. I could well represent the fields on create , but on update it would not be easy to fill in these fields (maybe), and the search function will not be very effective with this alternative.

So, I am looking for a third alternative, so I can fix my problem. What would be an effective way to solve this problem and be able to have categories with different input fields, as well as be able to effectively perform searches on these categories?

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

+6
source share
1 answer

For this scenario, I would:

  • create a category table to store each category
  • create a category_fields table to store each category field
  • create a compiled_category table to save all collected data from category fields in a serialized hash

The collected data can be easily (de) serialized into a text column (regardless of the db mechanism you use).

Check out the sources that use your problem: dynamic forms

+1
source

Source: https://habr.com/ru/post/979241/


All Articles