File for modifying the SQL query for create 'post'?

AI-generated summary

charnelpezai asks how to modify the SQL query for creating a post to include a new meta_tag_id column in the posts table. They are unsure how to integrate this into the PostController and SQL insertion process, as they are unfamiliar with Ruby. merefield advises against directly modifying the posts table and instead recommends using the PostCustomField model, which is designed for storing custom data safely. RGJ points to a guide on adding custom fields to models, and merefield shares examples from plugins and emphasizes using Rails Active Record concepts. charnelpezai appreciates the guidance and acknowledges their lack of familiarity with Ruby and Rails.

Hi guys, to give some background, I’m trying to add another column to the table schema of posts table, I want to add another column which is meta_tag_id. I got to a point where I can pass the data that I want to store in the parameters of PostController, check the log below for reference(this is from the logs of d/rails s):

Started POST "/posts" for 127.0.0.1 at 2022-09-16 06:08:19 +0000
Processing by PostsController#create as */*
  Parameters: {"raw"=>"Another dummy reply, for testing purposes.", "unlist_topic"=>"false", "category"=>"5", "topic_id"=>"23", "is_warning"=>"false", "archetype"=>"regular", "typing_duration_msecs"=>"4500", "composer_open_duration_msecs"=>"11534", "featured_link"=>"", "shared_draft"=>"false", "draft_key"=>"topic_23", "meta_tag_id"=>"summary", "nested_post"=>"true"}

Now, what I want to achieve is to insert the meta_tag_id parameter :point_up: in another column of the posts table, for reference, here is a sample logs of the SQL query when inserting a record in my posts table:

Post Create (1.1ms)  INSERT INTO "posts" ("user_id", "topic_id", "post_number", "raw", "cooked", "created_at", "updated_at", "sort_order", "last_editor_id", "last_version_at", "word_count", "baked_at", "baked_version") VALUES (1, 23, 7, 'Another dummy reply, for testing purposes.', '<p>Another dummy reply, for testing purposes.</p>', '2022-09-16 06:08:22.283658', '2022-09-16 06:08:22.283658', 7, 1, '2022-09-16 06:08:22.293786', 6, '2022-09-16 06:08:22.283601', 2) RETURNING "id"

I would be grateful if you guys could point me in the right direction. I’m not familiar with ruby, so I’m having a hard time finding the find that I should modify to achieve the adding of a new column and incorporating the meta_tag_id variable to the values of the query. Thank you in advance guys! :smile:

Modifying such a key table directly is probably risky, consider instead using the PostCustomField model, it exists for exactly this kind of purpose: to attach bespoke custom data to Posts, usually in a plugin.

If you have performance issues then maybe you can revisit, but this is a much simpler and safer solution.

Thank you for the reply, okay so my meta_tag_id will be stored in the post_custom_field table by using the PostCustomField class? Then to access the stored data on the post stream, I should get the data of the post_custom_field table also and incorporate the meta_tag_ids to the posts? Am I getting the correct use case of the PostCustomField class?

See How to add custom fields to models

There is no example for Posts but there are examples for Topics and Categories and they work the same way.

Here are examples of their use:

and

Make sure you are familiar with Rails Active Record Model concepts, because you’d normally want to do everything via Ruby on Rails rather than directly on the database.

Thanks for the replies, I will look at the references and examples that you guys given. Oh, I’m not familiar with active model concept, or in ruby in general, yet. This is very helpful, thanks again guys. :folded_hands:

It’s Lego, for databases :brick: