Rails and MySQL - Data Types

0

I recently started using Ruby on Rails for a project, and I have questions about scaffolding and other DB features in Rails.

Here is a topic explaining data types in Active Record Migration link

How can I use other types of data present in MySQL, such as Set, Enum and so many others? Can I change tables directly through MySQL or do I have to follow exactly what Active Record migrations offer?

    
asked by anonymous 19.07.2016 / 20:15

2 answers

1

Rails uses ActiveRecord ( ORM ) to map all tables and attributes thereof. So for you to use Rails without using external libraries, yes you can only use the data offered by Rails migrations .

However you can extend these features using a Gem . For your case, if you need to use SET or ENUM of MySQL you can use the gem native_enum which, according to the documentation , adds these two types to the Rails migration. Ex:

create_table :balloons, :force => true do |t|
  t.enum "color", :limit => ['red', 'gold'], :default => 'gold', :null => false
  # or...
  t.column "size", :enum, :limit => ['small', 'medium', 'large']
end
    
25.07.2016 / 20:43
0

The idea of migration is to have registered the evolution of your database as scripts, this way you would have the management of database change documented, besides facilitating the database update along with application code, ease of application of many scripts and bench equalization between environments, for example. With migrations you can evolve or regress the bank based on historical evolution along with your code. I advise you not to directly tinker with the database unless it is specific tunning or management tasks, or you can not do with migrations (Rare exceptions).

In rails 4 you can do Enum as follows DOC .

Or you can run queries in migration, example .

    
20.07.2016 / 21:30