...a has_many, has_one or belongs_to association, the :inverse_of option in Rails tells ActiveRecord that they're two sides of the same association. Example with a has...

...to :forum, inverse_of: :posts end Knowing the other side of the same association Rails can optimize object loading so forum and forum.posts[0].forum will reference the same object...

github.com

...written as a Ruby Gem and was tested and evaluated against one Ruby on Rails project. This card will summarize and present the research results, the evaluation and the programmed...

...The tool should be suitable for modern web based development workflows with Ruby on Rails. Criteria Faster results of failing tests than random prioritization to lower regression testing costs.

Add gem 'database_cleaner' to your Gemfile. Then: Cucumber & Rails 3+ # features/support/database_cleaner.rb DatabaseCleaner.clean_with(:deletion) # clean once, now DatabaseCleaner.strategy = :transaction Cucumber::Rails::Database.javascript_strategy = :deletion Cucumber & Rails 2

...available cucumber-rails for Rails 2 automatically uses database_cleaner when cucumber/rails/active_record is required -- but only if transactional fixtures are off. To have database_cleaner work correctly: Add the attached...

Rails has generic error messages you can define in your locale .yml files. You may override those application-wide error messages using model or attribute scope like this: en: activerecord...

...find out which parts of your application are not tested. Integrating this in a rails project with rspec, cucumber and parallel_tests is easy. Add it to your Gemfile and...

...gem 'simplecov', require: false end Add a .simplecov file in your project root: SimpleCov.start 'rails' do # any custom configs like groups and filters can be here at a central place...

class World < ApplicationRecord; end class World::Earth < ApplicationRecord; end Without any further configuration, Rails will produce the following database table names. >> World.table_name => "worlds" >> World::Earth.table_name => "world_earths...

tl;dr Prefer request specs over end-to-end tests (Capybara) to joyfully test file downloads! Why? Testing file downloads

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

api.rubyonrails.org

...is included in migrations by default (also see How to write complex migrations in Rails). Handy methods Note that both keys and values are always strings, i.e. you'll need...

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

github.com

...response time of a notoriously slow JSON API endpoint that was backed by a Rails application. While every existing app will have different performance bottlenecks and optimizing them is a...

The data flow examined in this card are based on an example barebone rails app, which can be used to reproduce the results. I tried to mimic the case...

It is very common to parse dates from strings. It seems obvious to use Date.parse for this job. However this...

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

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

...a clever migration, possibly by embedding the model into the migration script. Open the Rails console after deployment and re-save every single record. You should probably add two chores...

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.

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

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

...our applications. Hunting it down, we found that the memory leak was located in Rails' #prepend_view_path. It occurs when the instance method prepend_view_path is called in...

...in your ApplicationController, you can just use #prepend_view_path as before. Note that Rails 7 version differs slightly. Known affected Rails versions (maybe more): Rails 4.0 - 7.0

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

makandra dev

Rails log files rotate automatically when they reach approx. 100MB: $ ls -lh log/ -rw-r--r-- 1 user group 55M Sep 15 09:54 development.log -rw-r--r-- 1 user...

...This behavior is a built-in feature of Ruby's standard Logger class, which Rails uses by default. To control the maximum file size, set config.log_file_size in your...

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

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