Ga naar inhoud

Rails tips

Handige commando's

Verbind lokaal met je console

./bin/rails console

Gebruiker rol updaten (console)

user = User.find_by_email("email-address@lemone.com")
user.update!(role: "admin")

Gebruiker confirmen (via confirmable van Devise) (console)

user = User.find_by_email("email-address@lemone.com")
user.confirm

Toegang tot de Rails console (via Cloud66)

cx ssh -s 'Leuke recepten app productie' -e production dragon
cd $RAILS_STACK_PATH
./bin/rails c

Locale admin user aanmaken (console)

user = User.create!(email: "admin@admin.local", password: "adminadminadmin")
user.confirm

Export Postgres database (in je shell)

 pg_dump -F c -v -U postgres -h localhost leu_app_development -f dump.psql

Import Postgres database (in je shell)

pg_restore -c -C -F c -v -U postgres /tmp/<filename>.psql

Import database (in je shell via rails command)

bin/rails db < sql-file.sql

Kill all PostgreSQL connections

Filename db.rake:

namespace :db do
  desc "Kick out PostgreSQL users from the database."
  task kick: [:environment] do
    begin
      ActiveRecord::Base.connection.execute <<-SQL
      SELECT pg_terminate_backend(pg_stat_activity.pid)
      FROM pg_stat_activity
      WHERE pg_stat_activity.datname = '#{ActiveRecord::Base.connection.current_database}';
    SQL
    rescue ActiveRecord::StatementInvalid
      puts "All connections killed."
    end
  end
end