Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass default value to rails generate migration?

I want to know if I can pass a default value to the rails g migration command. Something like:

 $ rails generate migration add_disabled_to_users disabled:boolean:false #where false is default value for disabled attribute 

in order to generate:

class AddDisabledToUsers < ActiveRecord::Migration   def change     add_column :users, :disabled, :boolean, default: false   end end 
like image 346
Leantraxxx Avatar asked Jul 04 '14 01:07

Leantraxxx


People also ask

How does Rails know which migration to run?

Rails uses this timestamp to determine which migration should be run and in what order, so if you're copying a migration from another application or generate a file yourself, be aware of its position in the order. This generator can do much more than append a timestamp to the file name.

How does Rails migration work internally?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.


1 Answers

You can't: https://guides.rubyonrails.org/active_record_migrations.html#column-modifiers

null and default cannot be specified via command line.

The only solution is to modify the migration after it's generated. It was the case in Rails 3, still the case in Rails 6

like image 198
Benj Avatar answered Sep 23 '22 13:09

Benj