PG::DuplicateTable: ERROR: relation "table_name" already exists

StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR:  relation "table_names" already exists
: CREATE TABLE "table_names" ("id" serial primary key, "name" character varying, "owner_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL) 

This error show when you try to migrate a while the table is already existing.

This error is a result of deleting migration files instead of using the the rails rollback.

Solution

Delete the table using your console

your_app_development=# drop table table_name;
DROP TABLE

 Rails: Generate Model without Migration file

Rails allows you to generate models. Rails model generation will also generate a migration file which lets you generate the table and fields on the designated table. This is a normal `Rails generate model`

$ rails g model YourModel
      invoke  active_record
      create    db/migrate/20170507051508_create_your_models.rb
      create    app/models/your_model.rb
      invoke    rspec
      create      spec/models/your_model_spec.rb
      invoke      factory_girl
      create        spec/factories/your_models.rb

To generate a model without a migration file:

$ rails g model YourModel --migration=false
      invoke  active_record
      create    app/models/your_model.rb
      invoke    rspec
      create      spec/models/your_model_spec.rb
      invoke      factory_girl
      create        spec/factories/your_models.rb

The key is to set the --migration to false

 Print the Page content in Capybara

Capybara is arguably one of the most used tools in testing in Ruby on Rails. If you need to show the content of the page you are testing, you can use the following command

page.body

This is helpful if you want to see what the problem is with your tests.

There is another really helpful command that you can use.

save_and_open_page
=> #<Process::Waiter:0x007fe07aecbbe0 sleep>

This will open a web browser page and show the content. You wouldn’t see any design here.

Screen shot 2017 04 26 at 8.45.51 pm

 Setting Rails Environment Variables

It is always a better idea for configuration variables to stay on the server as environment variables instead of putting them on your repository. It makes it less likely for someone to have access to them. Here are a few ways to set environment variables for your Rails application.

~/.bashrc File

This is the simplest and most obvious of the 3. It is default way you are going to do it. Just add the environment variables in your ~/.bashrc file.

export APPLICATION="Application Name"
export APPLICATION_VERSION="Application Version"

dotenv

dotenv is a Ruby gem to load environment variables from an env file.

Installing

gem 'dotenv-rails', groups: [:development, :test]

And then execute:

$ bundle

figaro

Figaro is a nice gem that simplifies configuration files.

gem "figaro"

You might need to add the additional yml file to .gitignore.

# config/application.yml

application_conf: "882354"
application_key: "fausdhf7(0sadfsmd__nfasdf"
some_conf: "23456789hdsfdsfasdf132"

These can be used throughout the application via ENV["application_key"]