14 Dec 2022
Since the removal of the free Heroku plan, most of my throw-away prototype apps that I deployed have been shutdown. However, there are a few of them that don’t use a database that I expected to continue to work when Heroku shut off the database but didn’t.
I did a little research and found that you can reconfigure a Rails app to not use a database. This allowed my apps to run purely on the new Eco dynos, without paying for the database connection.
The steps I followed to reconfigure my Rails 6 app are as follows:
Comment out/remove the following:
// package.json
"@rails/activestorage": "^6.0.0",
// app/javascript/packs/application.js
require("@rails/activestorage").start()
# bin/setup
system! 'bin/rails db:prepare'
# config/environments/development.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.migration_error = :page_load
config.active_record.verbose_query_logs = true
# config/environments/test.rb
config.active_storage.service = :test # For Rails >= 5.2
# config/environments/production.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.dump_schema_after_migration = false
# spec/rails_helper.rb
ActiveRecord::Migration.maintain_test_schema!
# test/test_helper.rb
fixtures :all # In case you're using fixtures
# Only for Rails >= 5.0
#config/initializers/new_framework_defaults.rb
Rails.application.config.active_record.belongs_to_required_by_default = true
Delete app/models/application_record.rb
.
You can run your Rails app in production mode locally to make sure there are no errors.
RAILS_ENV=production rails assets:clobber
RAILS_ENV=production rails webpacker:compile # assuming you have webpacker
RAILS_SERVE_STATIC_FILES=1 RAILS_ENV=production rails s
If there are no errors (with the exception of missing Heroku ENV
keys), then you should be good to go!
If you are creating a new Rails app from scratch and don’t want to use a database, you can run rails new
with the --skip-active-record
flag.
27 Nov 2022
I’ve been working in a Rails 5 app recently and ran into some strange issues with the rails console
that I didn’t fully understand. Random queries or Ruby expressions would crash the console without allowing me to see the result. Some of these commands weren’t super urgent, I could write the query and display the results on a webpage just fine. Other times I could perform the work in a different IRB, which also worked. Eventually, after I was unable to run generators, I was annoyed enough to investigate, which was difficult.
[4] pry(main)> Octokit.repository?("appdev-projects/photogram-final")
/home/jello/.rvm/gems/ruby-2.7.3/gems/pry-0.10.4/lib/pry/exceptions.rb:29: warning: $SAFE will become a normal global variable in Ruby 3.0
/home/jello/.rvm/gems/ruby-2.7.3/gems/pry-0.10.4/lib/pry/exceptions.rb:29: warning: $SAFE will become a normal global variable in Ruby 3.0
Traceback (most recent call last):
/home/jello/.rvm/gems/ruby-2.7.3/gems/spring-2.0.2/lib/spring/application.rb:171:in `fork': cannot load such file -- yajl (LoadError)
/home/jello/.rvm/gems/ruby-2.7.3/gems/spring-2.0.2/lib/spring/application.rb:171:in `fork': undefined method `reject!' for nil:NilClass (NoMethodError)
/home/jello/.rvm/gems/ruby-2.7.3/gems/spring-2.0.2/lib/spring/application.rb:171:in `fork': undefined method `[]' for nil:NilClass (NoMethodError)
/home/jello/.rvm/gems/ruby-2.7.3/gems/spring-2.0.2/lib/spring/application.rb:171:in `fork': undefined method `reject!' for nil:NilClass (NoMethodError)
/home/jello/.rvm/gems/ruby-2.7.3/gems/spring-2.0.2/lib/spring/application.rb:171:in `fork': undefined method `reject!' for nil:NilClass (NoMethodError)
It appeared to be both a Spring issue and a Windows issue. The first few suggestions just said to stop spring with spring stop
then run whatever command that was not working again. This didn’t work for me (spring would auto-start). Updating spring didn’t resolve the issue either, as I could only update so much on a Rails 5.0 app.
The only thing that did work was disabling spring with an environment variable.
DISABLE_SPRING=1 rails console
Thanks to this SO answer.
21 Nov 2022
It’s fairly common that I’m in a situation like this:
class Submission < ApplicationRecord
belongs_to :student
end
class Student < ApplicationRecord
has_many :submissions
end
And I need to do something like:
submission.student.full_name
which really isn’t the worst. BUT there is a neat way you can Rails will let you call full_name
directly on submission
, using delegate
(which I always forget about).
class Submission < ApplicationRecord
belongs_to :student
delegate :full_name, to: :student
end
class Student < ApplicationRecord
has_many :submissions
end
and that makes it possible to do submission.full_name
19 Oct 2022
I ran into an issue creating a printable cheatsheet with code blocks. Sometimes code blocks would get cut off on different pages, which made the cheatsheet hard to read.
I was informed that you can prevent this behavior using the CSS page-break-inside
property.
Simply adding:
<style>
@media print {
.code-block, .row {
page-break-inside: avoid;
}
}
</style>
to my HTML prevented the page breaks from happening where I didn’t want them 🙂
13 Oct 2022
Oftentimes when I’m experimenting in a project I make a bunch of changes and then want to keep some of them (without committing) but undo the rest. I would try to stage the changes that I didn’t want and them stash them like this:
git add path/to/undesired/changes
git stash
But this would stash everything, including the changes I wanted to keep.
I tried being more specific with stash bu providing a path, but no dice.
$ git stash path/to/undesired/changes
fatal: unknown subcommand: path/to/undesired/changes
I think in an older version of Git this used to work, but I found the updated command today: git stash push _path_ --keep-index
$ git stash push path/to/undesired/changes --keep-index
Saved working directory and index state WIP on _branch_name_: 32dbc82 .
Thanks to this post.