[RUBY] What is the data structure of ActionText?

I personally had the opportunity to introduce ʻAction Text, so make a note. When developing something like a blog service, I was wondering how to introduce ʻActionText into the ʻArticle` model, so I hope it helps someone.

What is Action Text?

One of the features that has been available since rails6. Introduce rich text content and editing capabilities to Rails.

What I want to do this time

I would like to create an article posting service. Create an Article model with three data, title, description, and body, and introduce ActionText to edit the body.

Data structure

ʻCreating a model that introduces ActionText`

ʻGenerates an Articlemodel. title is the title of the article and description` is the description of the article. Note that we do not create columns to store the body data.

rails g model Article title:string description:text

ʻIntroduce ActionText`

ʻActionText` can be installed with the following command.

rails action_text:install

When you install ActionText, two migration files are generated. One is a file related to ʻActive Storage, and the other is a file that creates a table of ʻActionText. The latter table is the key to the data structure we want to discuss in this article.

Rich text content is stored in its own RichText model, which is associated with any existing Active Record model in your application. All embedded images (and other attachments) are automatically stored in Active Storage and associated with the included RichText model.

https://railsguides.jp/action_text_overview.html#%E3%81%AF%E3%81%98%E3%82%81%E3%81%AB

class CreateActionTextTables < ActiveRecord::Migration[5.2]
  def change
    create_table :action_text_rich_texts do |t|
      t.string     :name, null: false
      t.text       :body, limit: 16777215
      t.references :record, null: false, polymorphic: true, index: false

      t.datetime :created_at, null: false
      t.datetime :updated_at, null: false

      t.index [ :record_type, :record_id, :name ], name: "index_action_text_rich_texts_uniqueness", unique: true
    end
  end
end

By associating the above data with the ʻArticle` model, it is possible to edit the content of the article with rich text. Linking the data is easy, add it to the model as follows.

article.rb


class Article < ApplicationRecord
  has_rich_text :content
end

What kind of data structure is it?

As explained above, the ʻArticle table holds titleanddescription, and the content of the article (content) is kept in the table ʻActionText :: RichText. If you look at the results of the console below, it will be easier to imagine.

#Get Article model data
irb(main):001:0> Article.all.first
=> #<Article id: 1, created_at: "2020-07-11 13:19:00", updated_at: "2020-07-11 13:19:00", title: "test", description: "testです">

#ActionText associated with the Article model::Get RichText data
irb(main):002:0> Article.all.first.content
=> #<ActionText::RichText id: 1, name: "content", body: #<ActionText::Content "<div class=\"trix-conte...">, record_type: "Article", record_id: 1, created_at: "2020-07-11 13:19:00", updated_at: "2020-07-11 13:19:00">

#How to get the content of an article?
irb(main):003:0> Article.all.first.content.body
=> #<ActionText::Content "<div class=\"trix-conte...">

Recommended Posts

What is the data structure of ActionText?
'% 02d' What is the percentage of% 2?
What is testing? ・ About the importance of testing
What is the pluck method?
What is the BufferedReader class?
Rails6: Input the initial data of ActionText using seed
What is the constructor for?
What is the initialize method?
What is the representation of domain knowledge in the [DDD] model?
What kind of StringUtil is good
Filter the fluctuations of raw data
What is it? ~ 3 types of "no" ~
What kind of method is define_method?
Want to know what Ruby n is the power of 2? (Power judgment of 2)
What is @Override or @SuppressWarnings ("SleepWhileInLoop") in front of the function? ?? ??
data structure
What is the best file reading (Java)
What are the updated features of java 13
What is the Facade pattern useful for?
The contents of the data saved by CarrierWave.
What is Pullback doing in The Composable Architecture
What is Cubby
What is Docker?
Learn more clearly with diagrams !! Model structure of Web application What is MVC model?
What is null? ]
ActiveSupport underscore is not the inverse of camelize
Add empty data to the top of the list
What is java
What is Keycloak
What is the Java Servlet / JSP MVC model?
What is maven?
The order of Java method modifiers is fixed
What is Jackson?
What is different from the PHP language. [Note]
What is Docker
What is self
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is the volatile modifier for Java variables?
What is Gradle?
What is POJO
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
What is Java
What is centOS
What is RubyGem?
What is programming?
What are the advantages of DI and Thymeleaf?
What is before_action?
What is Docker
Retrieve the first day of week in current locale (what day of the week is it today?)
The official name of Spring MVC is Spring Web MVC
What is Byte?
What is Tomcat
The essence of AspectJ's mood-why your `@Transactional` is ignored
Existence check of has_many and belongs_to-optional: What is true?
When the hover of Eclipse is hard to see
In Time.strptime,% j (total date of the year) is
What is the difference between a class and a struct? ?? ??