List of Rails Model Types

Ruby on-Rails

Ruby on-Rails Problem Overview


Does someone have a complete list of model types that be specified when generating a model scaffolding

e.g.

foo:string 
bar:text 
baz:boolean

etc...

And what do these types map to in terms of default UI elements? Text field, Text area, radio button, checkbox, etc...

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGordon PotterView Question on Stackoverflow
Solution 1 - Ruby on-RailsBayard RandelView Answer on Stackoverflow
Solution 2 - Ruby on-RailsEdu LomeliView Answer on Stackoverflow