Learn Ruby on Rails: Learn Ruby on Rails Series Part 6

After learning how to create simple task apps using scaffolding, the next discussion this time is about tables and dummy data. For this discussion, use the previous project, as we will continue the application in a more integrated application.

Daftar Isi

A. Table definition in database

Say database And painting we have heard it often. A database is briefly analogous to a filing cabinet that is used to store data. Then the table is an archive, where in the archive some data is located.

B. Creating a table in the Rails framework

$ rails generate migration CreateCategories

What will be produced by the generator is the migration file in the folder database/migrate by name create_categories.rb. The contents of the file are as follows.

class CreateHomework [5.1]

def change

create_table :homework TO DO |t|

t.text :duty_name

t.chain :category

t.date :deadline

t.text :description

t.timestamps

END

END

END

Explanation:

  1. When creating a migration with the name Create categoriesthe migration file will be transformed into a class named CreateCategories.
  2. In the ActiveRecord section, this means that the migration is included in the ActiveRecord Rails section.
  3. def change
    
    end

    This method must exist before writing the script to create the table.
    In addition to method change, there are other methods, namely methods up And down. The structure of the migration file therefore becomes as follows. Method up And down used when make changes to the table.

    class Create categories [5.1]

    def up

    table_change :categories TO DO |t|

    END

    END

    def down

    table_change :categories TO DO |t|

    END

    END

    END

  4. create_table :categories do |t|

    This section means you will create a table with a name categories according to what is written, i.e. :categories in the migration file. Don’t forget to end with end.

The category migration file for the simple task management application has been created. Next, we add a script to write the necessary columns to the category table and their data types.

class Create categories [5.1]

def change

create_table :categories TO DO |t|

t.chain :name #create name column with type string

END

END

END


Besides this method, you can also add columns to the category table by writing the following script during the migration file creation process. Examples are as follows.

$ rails generate migration CreateCategories name:string
Advice:
When creating a table, also try adding a column which will later become an index in the table. This index will work if searching for data in the table can be done faster and more efficiently.

C. Rails Migration You Need to Know

Migration in Rails is not only for creating tables but can also modify, add or even delete columns.

If the creation of the migration is done using a generator, naming the migration file Also can be a quick way to define what the migration file will do.


An example is the following.

  1. Create tables
    Keywords for file names East CreateXXX.
    $ rails generate migration CreateCategories

    then it will produce

  2. Add columns to the table
    Keywords for file names East AddXXXHASXXX.
    $ rails generate migration AddDescriptionToCategories

    then it will produce

    $ rails generate migration AddDescriptionToCategories description:text

    then it will produce

  3. Delete a column in a table
    Keywords for file names East WithdrawXXXFromXXX.
    $ rails generate migration RemoveDescriptionFromCategories

    then it will produce

    $ rails generate migration RemoveDescriptionFromCategories description:text

    then it will produce


D. Data Types in Rails Migration

When creating a migration file in Rails, you need to pay attention to the data type used. Is it a string, text or int etc.. Here are the data types allowed in Rails migrations.

  • :chain,
  • :text,
  • :entire,
  • :float,
  • :decimal,
  • :datetime,
  • :timestamp,
  • :time,
  • :date,
  • :binary,
  • :boolean.

class CreateHomework [5.1]

def change

create_table :homework TO DO |t|

t.text :duty_name

t.chain :category

t.date :deadline

t.text :description

t.timestamps

END

END

END

E. Change method

The change method is the primary way to write migrations in Rails. However, this modification method can only be done in the following migration definition.

  • add_column
  • add_foreign_key
  • add_index
  • reference_added
  • add_timestamps
  • change_column_default
  • change_column_null
  • create_join_table
  • create_table
  • disable_extension
  • drop_join_table
  • drop_table
  • activate_extension
  • delete_column
  • delete_foreign_key
  • delete_index
  • delete_reference
  • delete_timestamps
  • rename_column
  • rename_index
  • rename_table

Here are some examples of the migration definition above.

#add_column

def change

add_column :categories, :description, :text

# add a description column to the categories table

END

#add_index

def change

add_index :categories, :name

#add an index to the category table, particularly in the name column

END

#remove_column

def change

delete_column :categories, :description, :text

# remove description column in category table

END

#rename_column

def change

rename_column :categories, :description, :desc

#rename the description column in the category table below

END

F. Rails migration modifiers

When editing or adding columns, sometimes the column requires a modifier. This modifier is useful for defining whether the column can be empty or not, whether the column is an index, whether the column is limited to a maximum of characters, etc. Fundamentally modifier described as Optional conditions in a column.

  • limit: Determines the maximum size of the string/text/binary/integer column.
  • precision: Defines the precision of the decimal column
  • ladder: shows how many digits appear after the sign (,)
  • polymorphic: Adds a column for relationships belongs_to (linked to membership)
  • null: Allow or not a column to be NULL
  • default: Allows you to assign a default value to a column when the column is not populated.
  • hint: Adds an index to a column.
  • comment: Add comments to a column.

After knowing a little about migration, we will then discuss dummy data in Rails.

G. What is dummy data?

Dummy data is like data fake or false data created for the purpose of testing the data. So how to do this in a Rails project?.


The main purpose of Rails migration, apart from modifying columns and tables, migration in Rails also allows adding or modifying data. To add data in Rails, Rails provides ‘seeds‘ built-in which makes the process of adding or modifying data fake be faster.


To do bootstrapping in Rails, you can do it on seeds.rb which is in the file db/seeds.rb.

After that, to deal with the seeds that have been written, follow these steps

To see if the boot process is successful, you can use rail console.

Rail console it’s a tool like irb (Interactive Ruby), but the difference is that it not only runs Ruby scripts, even Rails scripts can be run. To do this, run the following command in the terminal.


Then run it

$ category = Category.all

H. Seed and basic data

In addition to dummy data, a database must contain master data. What is master data?. Core data is like core data coming from data in other tables.

For example, if there is a table citythe table may contain primary data, because the data is only added once. If the additional process is done again, it can be done manually.

Why is it like that? Because if we think about the cities of Indonesia, for example, it is certain that will not increase every year No ?. This is why the city table can be basic data.

To add this master data, many developers use features seed of Rails, out of reflection seed is intended to add or modify data.

However, it’s one thing can’t done, because deep down seed This is a Rails feature that is useful for creating data fake. Master data is not data faketherefore avoid creating master data with seeding.


In conclusion, Rails makes it much easier for its users in many ways like creating tables, modifying tables or adding and modifying data to perform tests on tables. But for use seed, we must still remember the concept of seed himself.

PakarPBN

A Private Blog Network (PBN) is a collection of websites that are controlled by a single individual or organization and used primarily to build backlinks to a “money site” in order to influence its ranking in search engines such as Google. The core idea behind a PBN is based on the importance of backlinks in Google’s ranking algorithm. Since Google views backlinks as signals of authority and trust, some website owners attempt to artificially create these signals through a controlled network of sites.

In a typical PBN setup, the owner acquires expired or aged domains that already have existing authority, backlinks, and history. These domains are rebuilt with new content and hosted separately, often using different IP addresses, hosting providers, themes, and ownership details to make them appear unrelated. Within the content published on these sites, links are strategically placed that point to the main website the owner wants to rank higher. By doing this, the owner attempts to pass link equity (also known as “link juice”) from the PBN sites to the target website.

The purpose of a PBN is to give the impression that the target website is naturally earning links from multiple independent sources. If done effectively, this can temporarily improve keyword rankings, increase organic visibility, and drive more traffic from search results.

Jasa Backlink

Download Anime Batch