Heroku: Cannot run more than 1 Free size dynos

 Cannot run more than 1 Free size dynos.

This is a common error when you forget or cancel a connection to heroku when using commands like:

heroku run rake db:migrate
bc(language-bash). heroku run  rails c --app you_app_name

Check your sessions

You will need to check if you have any sessions running with:

$ heroku ps --app your_app_name
=== web (Free): bundle exec puma -C config/puma.rb (1)
web.1: up 2017/06/13 10:23:38 +0800 (~ 33m ago)

=== worker (Free): bundle exec sidekiq -c 5 -q mailers -q default (1)
worker.1: up 2017/06/13 10:23:19 +0800 (~ 33m ago)

=== run: one-off processes (1)
run.2943 (Free): up 2017/06/13 10:39:57 +0800 (~ 17m ago): rails c

Stop the session

We will need to stop the session so that we can create a new session which we can use.

heroku ps:stop run.2943 --app your_app_name

Try to run your session

$ heroku run rails c --app your_app_name
Running rails c on your_app_name... up, run.9284 (Free)
Loading staging environment (Rails 4.2.8)
[1] pry(main)>

Done!

 How to clear the contents of a file from the command line?

When developing, I usually have a log file for tests and development for me to keep track of what’s happening. The thing is that logs become too large. Some editors or viewers can’t open the file. They are sometimes too large to be usable.

I just delete the contents and fill it up until the next time.

Here are a few ways to delete file contents from the command line.

> filename

> log/development.log

echo

echo "test" > log/test.log

cat

cat /dev/null > log/test.log

There are probably millions of ways to do this but the above are the quickest

cp

cp /dev/null log/development.log 

 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