By activating strict_loading you force developers to address n+1 queries by preloading all associations used in the index...

Development environment setup Rails Composer Basically a comprehensive Rails Template. Prepares your development environment and lets you select web server, template engine, unit and integration testing frameworks and more.

...in minutes using an application template. With all the options you want! Code generators Rails Bricks A command line wizard. Once you get it running, it creates sleek applications.

Rails wraps your parameters into an interface called StrongParameters. In most cases, your form submits your data in a nested structure which goes hand in hand with the strong parameters...

...expect/permit/require when passing them to e.g. ActiveRecord. Note that expect is only available from Rails 8. There are cases where it might be fine to read directly, like: # Okay

#pluck is commonly used as a performant way to retain single database values from an ActiveRecord::Relation Book.pluck(:title, :price...

TL;DR: Rails ships two methods to convert strings to constants, constantize and safe_constantize. Neither is safe for untrusted user input. Before you call either method you must validate...

...w[User Post Test]) if class_name class_name.safe_constantize.new # either User, Post or Test else Rails.logger.error "This should not happen!" end Handling unresolvable constants The safe_constantize method is defined in...

Rails default config uses the ActiveSupport::Cache::NullStore and disables controller caching for all environments except production: config.action_controller.perform_caching = false config.cache_store = :null_store If you want to test caching...

path end end RSpec.configure do |config| config.include CachingHelpers end 3. Now stub the Rails cache and clear it before each example: RSpec.describe SomeClass let(:file_cache) { ActiveSupport::Cache.lookup_store...

This cards describes an example with a Github Client on how to keep your Rails application more maintainable by extracting domain independent code from the app/models folder to the lib...

...scenarios and not limited to API clients. Example Let's say we have a Rails application that synchronizes its users with the Github API: . └── app └── models ├── user │   ├── github_client.rb │   └── sychronizer.rb └── user.rb...

We are using assignable_values for managing enum values in Rails. Nevertheless Rails is adding more support for enum attributes, allowing to have a closer look at the current feature...

...to our still preferred option assignable_values. Active Record enum attribute interface By default Rails is mapping enum attributes to integers: class Conversation < ActiveRecord::Base enum :status, [ :active, :archived ]

This card will teach you how to index, search and rank your Rails models in a PostgreSQL full-text index. We will do this without using any gems...

Why Rails has multiple schema formats When you run migrations, Rails will write your current database schema into db/schema.rb. This file allows to reset the database schema without running migrations...

...by running rails db:schema:load. The schema.rb DSL can serialize most common schema properties like tables, columns or indexes. It cannot serialize more advanced database features, like views, procedures...

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.

...under Selenium WebDriver is super-painful. It's much easier to detect the current Rails environment instead. You might be better of checking against the name of the current Rails...

...in a data-environment of your . E.g., in your application layout: <html data-environment=<%= Rails.env %>> Now you can say in a piece of Javascript: if (document.documentElement.dataset.environment == 'test') { // Code that should...

...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...

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...

As your Rails project grows, you will accumulate a number of small patches. These will usually fix a bug in a gem, or add a method to core classes.

...change_storage.rb fix_cache_ids.rb sanitize_filename_characters.rb ruby/ range/ covers_range.rb array/ dump_to_excel.rb xss_aware_join.rb enumerable/ collect_hash.rb natural_sort.rb string/ to_sort_atoms.rb rails/ find_by_anything.rb form_builder.rb form_for_with_development_errors.rb Note how all patches for standard library classes are in the ruby...

After an upgrade to rails 7 I noticed that async reindexing jobs of Searchkick were failing for Model.reindex(mode: :async, wait: true): /home/a_user/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/searchkick-5.3.1/lib/searchkick/relation_indexer.rb:142:in `block in batch_job': undefined...

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...

...you most likely want to use this in combination with the ActionDispatch::AssumeSSL middleware (Rails >= 7.1). This middleware makes your app assume that SSL terminates at the load balancer and...

...custom middleware to automatically flag all cookies as secure-only In a Ruby on Rails app you can add a middleware that automatically sets the Secure flag to all server...

...See this ES6 compatibility matrix for details. ES6 and Uglifier If you're using Rails with the assets pipeline (sprockets) you are probably using Uglifier to minify your JavaScript.

...can check if that's an issue for your project by running bundle exec rails assets:precompile in your development environment (don't forget to run bundle exec assets:clobber...

...so please feel free to contribute! General workflows The official guide How to upgrade Rails: Workflow advice Upgrading Rails: Example commits Upgrade to Rails 7 Don't use log level...

...debug in your production environments Rails 7.1: Take care of the new production log default to standard out Upgrade to Rails 6 Don't use log level :debug in your...

tekin.co.uk

Git diffs show the surrounding contexts for diff hunks. It does so by applying regular expressions to find the beginning...

...need to decide, which configuration between different environment works good for you. By default Rails uses these settings for your application: require(:user) raises in all environments ActionController::ParameterMissing if...

Since Rails 6.1, if we use where.not with multiple attributes, it applies logical NAND (NOT(A) OR NOT(B)) instead of NOR (NOT(A) AND NOT(B)). If you do...

...becomes "Don't send newsletters to trashed admins". User.where.not(role: 'admin', trashed: true) # Before Rails 6.1, with NOR => "SELECT "users".* FROM "users" WHERE "users"."role" != 'admin' AND "users"."trashed" != TRUE...

...posts with a limited number of tags. The following chapters explain different approaches in Rails, how you can assign such an association via HTML forms. In most cases you want...

...with assignable values. The basic setup for all options looks like this: config/routes.rb Rails.application.routes.draw do root "posts#index" resources :posts, except: [:show, :destroy] end db/migrate/20230510093740_create_posts.rb class CreatePosts < ActiveRecord::Migration...