--What is ActiveModel :: Model? --Is it useless to inherit Application Record? --Reference
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
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.
--Rails Guide
Recommended Posts