How to reverse a 'rails generate'
rails destroy controller lalala
rails destroy model yadayada
rails destroy scaffold hohoho
Rails 3.2 adds a new d
shortcut to the command, so now you can write:
rails d controller lalala
rails d model yadayada
rails d scaffold hohoho
What is the best way to drop a table & remove a model in Rails 3?
The second method is the ideal way to handle this: your migration files are meant to represent how your database has changed over time. The older migration files will remain in your project (in case, hypothetically, you wanted to roll back to an older version), but Rails will not run them when you rake db:migrate
because it knows they've already been run (based on data in the database's schema_migrations table).
Your schema.rb will just be updated once to reflect that your database no longer contains that table.
One minor tweak to your code: your migration file should drop the table in the up
method, and ideally recreate it in the down
method. The "up" signifies that your migration drops the table to move forward in time, and if the migration is rolled back, the down
method will be run.
Undo scaffolding in Rails
First, if you have already run the migrations generated by the scaffold
command, you have to perform a rollback first.
rake db:rollback
You can create scaffolding using:
rails generate scaffold MyFoo
(or similar), and you can destroy/undo it using
rails destroy scaffold MyFoo
That will delete all the files created by generate
, but not any additional changes you may have made manually.
Rails destroy scaffold without previous rails g scaffold
I discover that destroy scaffold, only generetes paths to remove files, for example:
rails d scaffold my_directory/something
That generetes the next output in console:
invoke active_record
remove db/migrate/20170901061811_create_my_directory_somethings.rb
remove app/models/my_directory/something.rb
invoke test_unit
remove test/models/my_directory/something_test.rb
remove test/fixtures/my_directory/somethings.yml
invoke resource_route
route namespace :my_directory do
resources :somethings
end
invoke scaffold_controller
remove app/controllers/my_directory/somethings_controller.rb
invoke haml
remove app/views/my_directory/somethings
remove app/views/my_directory/somethings/index.html.haml
remove app/views/my_directory/somethings/edit.html.haml
remove app/views/my_directory/somethings/show.html.haml
remove app/views/my_directory/somethings/new.html.haml
remove app/views/my_directory/somethings/_form.html.haml
invoke test_unit
remove test/controllers/my_directory/somethings_controller_test.rb
invoke helper
remove app/helpers/my_directory/somethings_helper.rb
invoke test_unit
invoke jbuilder
remove app/views/my_directory/somethings
remove app/views/my_directory/somethings/index.json.jbuilder
remove app/views/my_directory/somethings/show.json.jbuilder
remove app/views/my_directory/somethings/_my_directory_something.json.jbuilder
invoke assets
invoke coffee
remove app/assets/javascripts/my_directory/somethings.coffee
invoke scss
remove app/assets/stylesheets/my_directory/somethings.scss
invoke scss
That generates some paths where the files should be, and try to delete that files if exist, don't mind if you create that files with scaffold or manually. But if that some files doesn't exist it only skip that files without advise in console output
In my app that files wasn't create in the correct directory so rails d scaffold can't remove it
Rails DB Migration - How To Drop a Table?
You won't always be able to simply generate the migration to already have the code you want. You can create an empty migration and then populate it with the code you need.
You can find information about how to accomplish different tasks in a migration here:
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
More specifically, you can see how to drop a table using the following approach:
drop_table :table_name
Removing a Devise user model possible?
I don't think devise offers a generator that would bring your model back to a previous version (similar to rails destroy migration ...
). Apart from removing the devise
method call from your model, you would need to get rid of the table itself (in a migration) and all the generated templates and controllers referring to the Student
model, just to keep things clean.
Deleting empty asset files in Rails
rails generate scaffold
is just a helper. It simply generated everything that could possibly be useful to you.
Sure you can delete all unneeded files. You could also delete a model or controller after it has been created if you don't need it anymore.
How do I Create a Rails Model from a Subset of Table Records
This is called Single Table Inheritance (STI).
If you had a column named type
in your table, it would likely work automatically. But you can change this column name that Rails uses to tell types apart.
http://api.rubyonrails.org/classes/ActiveRecord/Base.html
Single table inheritance
Active Record allows inheritance by storing the name of the class in a column that by default is named “type” (can be changed by overwriting Base.inheritance_column). This means that an inheritance looking like this:
class Company < ActiveRecord::Base; end
class Firm < Company; end
class Client < Company; end
class PriorityClient < Client; end
When you do Firm.create(:name => "37signals"), this record will be saved in the companies table with type = “Firm”. You can then fetch this row again using Company.where(:name => '37signals').first and it will return a Firm object.
So, try this code
class Car < ActiveRecord::Base
set_table_name 'Vehicle_Table'
self.inheritance_column = :vehicle_type
end
Related Topics
How to Get a Particular Line from a File
Problem with Quantifiers and Look-Behind
How to Find Out Who Is Connected to Actioncable
Ruby Block to String Instead of Executing
How to Create a Nokogiri Case Insensitive Xpath Selector
How to Read a File from Bottom to Top in Ruby
In Ruby, Is There No Way to Dynamically Define a Local Variable in the Current Context
Trying to Install Ruby-Filemagic on Snow Leopard Using Brew Rather Than Ports
Sort a Collection of Objects by Number (Highest First) Then by Letter (Alphabetical)
How to Check If a Ruby Array Includes One of Several Values
Ruby Backslash to Continue String on a New Line
Show Full Path Name of the Ruby File When It Get Loaded
Error: While Executing Gem ... (Zlib::Gzipfile::Error) Not in Gzip Format