Learn Ruby on Rails: Learn Ruby on Rails Series Part 7

Having previously understood the use of migration, seeds and how to create dummy data, as well as the differences between dummy data and master data. This time we will understand Active registration.

Active recording East library-library ORM which is used on model di Rails Framework. Model that’s what it is M of the concept MVC on Rails. It serves as the model responsible for business data and logic. Examples of business data include people data, location data, or goods data.

If implemented, this Active Record is an ORM (Object Relational Mapping) system in Rails.

Daftar Isi

A. Definition of ORM

ORM (Object Relational Mapping) is a programming method/technique used to convert data from an object-oriented programming language (OOP) environment to a relational database environment.

In short, ORM can be thought of as a bridge that works to connect OOP and relational databases.

B. What can Active Record do?

Active recording has the ability to

  • Represents the model and model data.
  • Represents relationships between models.
  • Shows the inheritance hierarchy of associated models.
  • Validate the data before entering it into the database.
  • Displays database operations in OOP form.

C. Naming Provisions

By default, Active Record requires theme layouts to determine the mapping of models and tables. Example The naming between models and tables in Rails is as follows. This example is based on the application we created from the Part 5 – Creating Your First Rails Application module, which is a simple task management application.
Model/Class Table/Diagram
Category categories
Homework homework


From the table, we can see that the naming of models and tables follows the following rules.

  • Database Table – Written in the plural, if more than two words are separated by an underscore.
    Example
    : homework, categories, homework_item
  • Model class – Written singularly with the first letter of each word capitalized
    C
    For example: Hhomework, category, homeworkItem

To quickly create templates and tables with correct naming conditions, you can use the generator method

$ rails generate model Homework

D. Naming Provisions Plan

Active Record also uses naming conventions for table columns used according to the purpose of the column. These provisions are as follows.

  • Foreign keys – This field must be named according to the name of the table used. For example item_id, order_id. This field will be automatically created by Active Record when determining the relationship between models.
  • Primary keysBasically By default, Active Record will use an integer column named ID as its primary key. When using Active Record, this column will be automatically created.


For an example of using Active Record, we will continue working on a simple task management application. If before we created a CRUD system using scaffolding, this time we will create a CRUD system for everyday task categories without using scaffolding.


E. CRUD uses Active Record without scaffolding

In the previous tutorial, we showed how to create a category table that only requires the category_name column using a builder. For those who don’t know how to do this, run the following command in the terminal.

$ rails generate migration CreateCategories
  1. Because in the previous tutorial the generator was only used to create migrations, the creation of the model will be done manually as follows.
    • Create a new file in the app/models folder with the name category.rb, then complete the following script.This will indicate that the Category model will reference the category table.
  2. After that create a controller with a name categories_controller.rb and fill it with the following script.
  3. Then in routes.rb add the following script.In the above script we register all the methods of categories_controller.rb become a resource.

Create an active Pada record

Next, to create a system or add data, we will do

  1. Create a folder on application/views by name categories and create a file with the name new.html.erb And _form.html.erb in the created folder.

    Remarks :
    The _form.html.erb file we create partial. In Rails, partial file names must be preceded by a sign emphasize.
  2. Try to start the server by running the command
  3. On file categories_controller.rb add the following script.
  4. On file _form.html.erb add the following script.
  5. Add script to file new.html.erb as follows.
The steps above only produce an add category page. Try running it again url If the above steps are performed correctly, the display on the add category data page will appear as follows.

After creating a process to display a new page, we will then create a process to save the data entered from that page.

  1. On the method create Of categories_controller.rb add the following script.
    Here is an explanation of the script:
    @category = Category.new(params.require(:category).permit(:name))
    

    Explanation:

    • Do instantiation has Model Categorywhere to instantiate this requires category parameter.
    • This parameter is found on the form new.html.erb. And this data allowed this is data name just.
    respond_to do |format| 
      if @category.save
        format.html { redirect_to "/categories/new", notice: "Category was successfully created." }
      else
        format.html { redirect_to "/categories/new", notice: "Category wasm't successfully created." }
      end
    end
    

    Explanation:

    • answer_to do useful for execute commands save data using web services support. This tag is useful for displaying response results in HTML format.
    • If the data is held by variables @category successfully registeredFor displays text in the form of the category name added successfully and return to the add data page
    • If not registered successfullywill displays text that the category name was not added successfully and return to the add data page.

    We’re doing tests, we’re trying to capture data physical in the text field, as follows.

    Try clicking the Create Category button. If the data is saved successfully, the screen will appear as follows.

Read active pada recording

Next, for the system to display all the category data that has been entered, we will do

  1. Create a file with the name index.html.erb in the file application/views/categories.
  2. Try to start the server by running the command
  3. On file index.html.erb add the following script.
  4. Then run the URL again in the browser. So, on this page, the display that will appear is as follows.

    Remember again that in the previous module i.e. Part 6 – Creating tables and dummy data, we added data to the category table via seeding in Rails. the data consist of Mathematics, history, Indonesian, English, Japanese, religion, chemistry and biology.

    While Physical are data added via the form.

What we do is display all category data, and what if we want to display specific category data. The measures taken are:

  1. Create a file with the name show.html.erb in the file application/views/categories. Add the following script.
  2. On categories_controller.rb method to showadd the following script.
  3. Try to start the server by running the command
    $ rails s

    and open the url and click the Show link on any of the data. So on this page the display that will appear is as follows

Update active record pada

Next, to create a system for modifying category data, we will do

  1. Create a file with the name edit.html.erb in the file application/views/categories. Add the following script.
  2. On categories_controller.rb method to modifyadd the following script.
  3. Try to start the server by running the command
    $ rails s

    and open the url and click the edit link on any of the data. So on this page the display that will appear is as follows

    We have just reached the display stage of displaying data changes. Next, we add a script to perform the data modification process.

    On categories_controller.rb method updateadd the following script.

    Now try to edit the data on the edit data page. If you perform the steps correctly, the data will definitely be modified successfully.

Next, we will create a process to delete the data. What to do is the following.

Delete active record Pada

Next, to create a system for modifying category data, we will do

  1. On categories_controller.rb destroy, add the following script.
  2. Try to start the server by running the command
    $ rails s

    and open the url and click the destroy link on any of the data. So on this page a popup will appear in the form of data deletion confirmation as follows


    If we press the OK button, the data will be deleted and we will return to the previous page.

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