What's the syntax for dropping a database table column through a Rails migration?
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- NOT DISTINCT query in mySQL
- Eager-loading association count with Arel (Rails 3
- Flush single app django 1.9
相关文章
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
In a rails4 app it is possible to use the change method also for removing columns. The third param is the data_type and in the optional forth you can give options. It is a bit hidden in the section 'Available transformations' on the documentation .
There are two good ways to do this:
remove_column
You can simply use remove_column, like so:
This is fine if you only need to make a single change to your schema.
change_table block
You can also do this using a change_table block, like so:
I prefer this as I find it more legible, and you can make several changes at once.
Here's the full list of supported change_table methods:
http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table
For instance:
would remove the hobby Column from the users table.
Do like this;
rails g migration RemoveColumnNameFromTables column_name:type
I.e.
rails g migration RemoveTitleFromPosts title:string
Anyway, Would be better to consider about downtime as well since the ActiveRecord caches database columns at runtime so if you drop a column, it might cause exceptions until your app reboots.
Ref: Strong migration
Through
remove_column :table_name, :column_name
in a migration file
You can remove a column directly in a rails console by typing:
ActiveRecord::Base.remove_column :table_name, :column_name
remove_column
inchange
method will help you to delete the column from the table.Go on this link for complete reference : http://guides.rubyonrails.org/active_record_migrations.html