rails-snippets-sql.md#

DB#

To drop a particular database, you can do this on rails console:#

$rails console
Loading development environment
1.9.3 > ActiveRecord::Migration.drop_table(:<table_name>)
1.9.3 > exit

And then migrate DB again

\$bundle exec rake db:migrate

xxx#

$ rake db:drop # deletes the database for the current env $ rake db:create # creates the database for the current env $ rake db:schema:load # loads the schema already generated from schema.rb / erases data $ rake db:seed # seed with initial data

1312

1 db:migrate runs (single) migrations that have not run yet. db:create creates the database db:drop deletes the database db:schema:load creates tables and columns within the (existing) database following schema.rb

db:setup does db:create, db:schema:load, db:seed

db:reset does db:drop, db:setup db:migrate:reset does db:drop, db:create, db:migrate

#

Because in development , you will always want to recreate the database,you can define a rake task in your lib/tasks folder like that.

  namespace :db do
      task :all => [:environment, :drop, :create, :migrate] do
   end
end

and in terminal you will run

rake db:all

it will rebuild your database

AAA#

BB#

remove table column from rails console#

ActiveRecord::Migration.remove_column(:table_name, :column_name)

#

fff#

namespace :schema do
  desc 'Creates a db/schema.rb file that is portable against any DB supported by Active Record'
  task :dump => [:environment, :load_config] do
    require 'active_record/schema_dumper'
    filename = ENV['SCHEMA'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema.rb')
    File.open(filename, "w:utf-8") do |file|
      ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
    end
    db_namespace['schema:dump'].reenable
  end

  desc 'Loads a schema.rb file into the database'
  task :load => [:environment, :load_config, :check_protected_environments] do
    ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV['SCHEMA'])
  end
# desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
  task :reset => [ 'db:drop', 'db:setup' ]
namespace :migrate do
# desc  'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
task :redo => [:environment, :load_config] do
  if ENV['VERSION']
    db_namespace['migrate:down'].invoke
    db_namespace['migrate:up'].invoke
  else
    db_namespace['rollback'].invoke
    db_namespace['migrate'].invoke
  end
end
namespace :db do
  task create: ["db:mysql:build", "db:postgresql:build"]
  task drop: ["db:mysql:drop", "db:postgresql:drop"]
end

Tricks#

The key thing to note is that #find returns the actual record while #where returns an ActiveRecord::Relation which basically acts like an array. So if you’re using #where to find a single record, you still need to remember to go into that “array” and grab the first record, e.g. User.where(email: "foo@bar.com")[0] or User.where(email: "foo@bar.com").first.