Rails and Mysql - Data Types

Asked

Viewed 256 times

0

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

Here’s a topic explaining data types in Active Record Migration https://stackoverflow.com/questions/17918117/rails-4-list-of-available-datatypes

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

2 answers

1

The Rails uses ActiveRecord (ORM) to map all tables and attributes of them. So to use Rails without using external libraries, yes you will only be able to use the data offered by Rails.

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 Gem native_enum which, according to the documentation, adds these two types to the migration of Rails. 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

0

The idea of Migration is to have registered as scripts the evolution of its database, in this way Voce would have the management of documented database change, besides facilitating the update of the database along with application code, ease in applying many scripts and database equalization between environments, for example. With Migrations Voce you can evolve or regress the bank based on historical evolution along with its code. I advise you not to touch the database directly unless it is a specific tunning or management task, or if you cannot do it with Migrations (Very rare exceptions).

In Rails 4 Voce you can do Enum as follows DOC.

Or Voce can run queries in Migration, example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.