[RUBY] Reasons to include ActiveModel :: Model to create a Form object

Overview

--What is ActiveModel :: Model? --Is it useless to inherit Application Record? --Reference

What is Active Model

You will be able to use functions that work with Action Pack and Action View.

According to the Rails guide, you will be able to use functions such as model name research, conversion, translation, and validation. You will also be able to initialize the object with a hash of the attributes in the same way as with Active Record.

If you write it in code

model


class NameList
  include ActiveModel::Model

  attr_accessor :name, :email, :prefecture

  validates :name, :email, :prefecture
end
 name_list = NameList.new(name: "Tarou", email: "[email protected]", prefecture: "Aomori")

  name_list.name # => "Tarou"
  name_list.email # => "[email protected]"
  name_list.prefecture # => "Aomori"

It ’s a rough interpretation, It seems that the defined attribute value can be validated and an instance with the attribute value as the hash key can be created.

One question arose here. Should I inherit the ApplicationRecord class, which already has similar functionality? When

Is it useless to inherit ApplicationRecord?

In conclusion, when transitioning to the View that prepares the form, an error occurs.

ActiveRecord::StatementInvalid
Mysql2::Error: Table 'sample_app_development.name_lists' doesn't exis

The cause of the error is thought to be that the table associated with the model does not exist.

It seems that ActiveRecord :: Base, the parent of ApplicationRecord, has a function to associate a model and a table on a one-to-one basis.

I think the reason for including ** ActiveModel :: Model ** is to minimize the acquisition of methods required for feature implementation.

When creating a Form object, it seems good to write the code according to the design pattern. If you have any doubts, it may be interesting to make an error and find the cause. I would appreciate it if you could let me know if there are any mistakes.

reference

--Rails Guide

Recommended Posts

Reasons to include ActiveModel :: Model to create a Form object
What to do if you accidentally create a model
Challenge to create inquiry form
How to create a method
How to create a form to select a date from the calendar
I want to create a form to select the [Rails] category
Preparing to create a Rails application
[Java] How to create a folder
Try to create a server-client app
How to create a Maven repository for 2020
Create a temporary class with new Object () {}
[Swift5] How to create a splash screen
[rails] How to create a partial template
A memo to simply create a form using only HTML and CSS in Rails 6
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
[Spring sample code included] How to create a form and how to get multiple records
How to create an Excel form using a template file with Spring MVC
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
How to create a database for H2 Database anywhere
[Android] Inherit ImageView to create a new class
[Rails] rails new to create a database with PostgreSQL
[Rails] How to create a graph using lazy_high_charts
Try to create a bulletin board in Java
How to create pagination for a "kaminari" array
How to create a class that inherits class information
I tried to create a LINE clone app
How to create a theme in Liferay 7 / DXP
What a beginner did to understand Object Orientation
[1st] How to create a Spring-MVC framework project
How to easily create a pull-down in Rails
[Rails] How to create a Twitter share button
How to create member variables with JPA Model
I tried to make a message function of Rails Tutorial extension (Part 1): Create a model