Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of Rails Model Types

People also ask

What are models in Rails?

A Rails Model is a Ruby class that can add database records (think of whole rows in an Excel table), find particular data you're looking for, update that data, or remove data. These common operations are referred to by the acronym CRUD--Create, Remove, Update, Destroy.

What are active models in Rails?

Active Model is a library containing various modules used in developing classes that need some features present on Active Record.

What are Ruby models?

What are models? In simple terms, models are Ruby classes that can holds value of a single row in a database table. Since they all inherit ActiveRecord::Base through ApplicationRecord class, they are equipped with all the ActiveRecord methods which enables them to interact with the database.

What is scaffolding in Ruby on Rails?

Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.


The attributes are SQL types, hence the following are supported:

  • :binary
  • :boolean
  • :date
  • :datetime
  • :decimal
  • :float
  • :integer
  • :primary_key
  • :string
  • :text
  • :time
  • :timestamp

These are documented under column in the Active Record API.


You can use the following basic field types in model scaffolding, all are supported in ActiveRecord supported databases without any extra gem (MySQL, PostgreSQL, SQLite):

  • :binary
  • :boolean
  • :date
  • :datetime
  • :decimal
  • :float
  • :integer
  • :primary_key
  • :string
  • :text
  • :time
  • :timestamp

In the scaffold generator you can also declare the foreign references using :references field type, which additionaly adds a belongs_to reference in the new model.

If you use Rails 4 and PostgreSQL, you can take advantage of these:

  • :hstore
  • :array
  • :cidr_address
  • :ip_address
  • :mac_address

For UI mapping (Model scaffold data type -> HTML), the next image has all the basic field types:

Rails data types, scaffolding HTML mapping