Ruby on Rails - playing with tables using rails console in ruby on rails- ruby on rails tutorial - rails guides - rails tutorial - ruby rails
Playing with tables using rails console
View tables
ActiveRecord::Base.connection.tables
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Delete any table.
ActiveRecord::Base.connection.drop_table("users")
------------OR----------------------
ActiveRecord::Migration.drop_table(:users)
------------OR---------------------
ActiveRecord::Base.connection.execute("drop table users")
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Remove index from existing column
ActiveRecord::Migration.remove_index(:users, :name => 'index_users_on_country')
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
where country is a column name in the migration file with already added index in users table as shown below:-
t.string :country,add_index: true
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Remove foreign key constraint
ActiveRecord::Base.connection.remove_foreign_key('food_items', 'menus')
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
where menus has_many food_items and their respective migrations too.
Add column
ActiveRecord::Migration.remove_column :table_name, :column_name
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Example
ActiveRecord::Migration.add_column :profiles, :profile_likes, :integer, :default => 0