When your Rails application is using Redis as its cache store, this is how you can list existing keys: Check: Rails.cache should return an ActiveSupport::Cache::RedisCacheStore. Rails.cache.redis.with(&:keys) lists...
Rails 5.2 soft-deprecated the storage of secrets in secrets.yml in favor of a new thing, credentials.yml.enc. Rails 7.1 deprecated secrets and Rails 7.2 finally removed it. In our permissions...
...for existing applications it may be appropriate to keep using secrets.yml. Restoring secrets in Rails 7.2+ Restoring Rails.application.secrets is really simple, thanks to config_for. Simply add this to config/application.rb...
...best practices to maintain your tasks in larger projects. Rake Tasks vs. Scripts The Rails default is using rake tasks for your application tasks. These live in lib/tasks/*.
end end Example for a script: The slim ruby script: lib/scripts/user_export.rb: # bundle exec rails runner -e development lib/scripts/user_export.rb Gitlab::UserExport.new.export The main code lib/scripts/gitlab/user_export.rb: module Gitlab class UserExport
...notice this until it's too late. We investigated multiple gems and found that rails_performance (https://github.com/igorkasyanchuk/rails_performance) provides a lot of valuable information with very little setup cost...
...a good default to add a unique index on the foreign key when using Rails’ has_one relationship. This ensures the database enforces the 1:1 constraint and raises an...
...older formatter API. Maybe there will be a fix for that eventually. Update cucumber-rails to >= 1.6.0: bundle update cucumber-rails Upgrade cucumber: bundle update cucumber Make sure you have...
Why and what to test 📘 Chapter "The value of tests" from Growing Rails Applications in Practice (in our library) 📄 Write tests. Not too many. Mostly integration. — the reasoning...
...strategy that fits — the different test-suite shapes side by side 📄 Guidelines for Testing Rails Applications — the same discussion for a Rails/RSpec suite RSpec and Capybara 📘 Everyday Rails Testing with...
Rails slightly changed the fragment cache implementation from Rails 7.0 to Rails 7.1. Unfortunately, this is incompatible with how Haml 5 patches Action View buffers. I tried turning a String...
...an ActionView::OutputBuffer, but this brought up other issues. Conclusion While we have a Rails 7.2 application successfully running with Haml 5, Rails applications with fragment caching need to upgrade...
Rails 7.1 added the normalizes method which can be used to normalize user input. It lets you define the fields you want to normalize and how to normalize them. In...
...validators or models (app/normalizers) you can use the following initializer. # config/initializers/normalizers.rb module Normalizers; end Rails.autoloaders.main.push_dir("#{Rails.root}/app/normalizers", namespace: Normalizers...
...reuse your existing factories instead of using the UI or creating records in the Rails console. This approach saves time and gives you useful defaults and associations right out of...
You can use FactoryBot directly in the Rails console like this: require 'factory_bot_rails' # Not needed if the factory_bot_rails gem is in the :development group...
...This is arguably a good idea. As a workaround, use stub_const in your Rails specs like this: stub_const "#{SomeClass}::CONST", 'test' This will invoke Rails' autoloading and fix...
Ruby and Rails have several methods for creating a new object that looks like another: clone, dup, deep_dup. When using them you should be aware of their differences so...
Rails 3, 4, 5, 6 config/application.rb config/environment.rb before the initialize! call (we don't usually edit this file) The current environment, e.g. environments/production.rb Gems Vendored plugins All initializers in config/initializers...
...initialize! call (we don't usually edit this file) Your own code from app Rails 2 Code in config/preinitializer.rb (if it exists) environment.rb, code above the Rails::Initializer.run block (put...
...to your DB schema. Ignore the column With ignored_columns you can prevent your Rails models from generating accessor methods for these columns: class User < ApplicationRecord self.ignored_columns += [:legacy_field...
#pluck is commonly used as a performant way to retain single database values from an ActiveRecord::Relation Book.pluck(:title, :price...
...end-to-end test for every small requirement. After we integrated Jasmine into a Rails app we often add an E2E test that opens that Jasmine runner and expects all...
If you're using a Redis cache in Rails (e.g. :redis_cache_store), it's possible to configure additional parameters for your Redis connection. Example config for Rails 7.2
...too low value for timeouts can make your cache timeout too often. Note that Rails explicitly sets connect_timeout, read_timeout and write_timeout, which means that only configuring timeout...
To reload a single-item association in Rails 5+, call #reload_ : post.reload_author In older Railses you can say post.author(true...
Icon fonts like Font Awesome are infinitely scalable, look great on high-DPI displays and will give your app a...
Rails' default logger prefixes each log entry with timestamp and tags (like request ID). For multi-line entries, only the first line is prefixed which can give you a hard...
...time when grepping logs. Example Rails.logger.info(<<~TEXT) Response from example.com: Status: 200 Body: It works! TEXT With that, the following is written to your log file.
RSpec Rails can automatically mix in different behaviors to your tests based on their type tag, for example enabling you to call get and post in specs with the tag...
If you are using the routing-filter gem in your Rails 7.1 app for managing URL segments for locales or suffixes, you will notice that the filters do no longer...
...and the necessary parameters are no longer extracted. That is because routing-filter patches Rails' find_routes-method to get the current path and apply its defined filters on it...
When using Rails credentials, you will edit the encrypted credentials for staging or production environments from time to time. To do that you need the secret key which should only...
...to live in :shared_path/config/credentials/:stage.key. If you have a single master.key (e.g. on Rails < 7.2), edit the Capistrano task to find the key at :shared_path/config/master.key instead. Usage
...ALLOW_REMOTE_DATABASE_URL: 'true' DATABASE_URL: postgres://postgres:postgres@localhost:5432/test PGTZ: 'Europe/Berlin' RAILS_ENV: test TZ: 'Europe/Berlin' strategy: matrix: partition: [ 0, 1, 2, 3 ] # Keep in sync with...
...uses: actions/checkout@v4 - uses: ./.github/actions/setup-node - uses: ./.github/actions/setup-ruby - name: Setup database schema run: bundle exec rails db:create db:schema:load - name: Precompile assets run: bundle exec rails assets:precompile