...transactions and locking. Examples will be given for the ActiveRecord ORM (from Ruby on Rails), but apply to all technologies. Use transactions to group related changes Use a transaction to...

...than a single database row, you should always use a transaction. Note that in Rails, ActiveRecord::Base#save automatically opens a transaction. Hence changes you make in callbacks, nested attributes...

...characters that have a particular meaning in URLs, like & or =. If you are using Rails URL helpers like movies_path(:query => ARBITRARY_STRING_HERE), Rails will take care of the...

Rails offers several methods to manage three types of different cookies along with a session storage for cookies. These are normal, signed and encrypted cookies. By following the happy...

...sparse and only focuses on controller specs, which recommended usage have been limited since Rails 5+ (see "Rails: Support for Rails 5"), this card will summarize some guidance on how...

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

axonflux.com

/^([\w!#$%&'*+-/=?^`{|}~]+.)*[\w!#$%&'*+-/=?^`{|}~]+@((((([a-z0-9]{1}[a-z0-9-]{0,62}[a-z0-9]{1})|[a-z]).)+[a-z]{2...

makandra dev

$ ruby -e "RubyVM::YJIT.enable; puts RubyVM::YJIT.enabled?" true The last variant is what Rails 7.2+ is doing after the boot process with its new default configuration. So if you...

...are on a current Rails version and didn't explicitly turn off YJIT, you are probably already using it. Keep in mind that Rails turns on YJIT after the boot...

Background information about session storage in Rails Rails has a default mechanism to store the session in the CookieStore. This is a cookie which holds the entire user session hash...

...to add associations across those records, if they are related in some way. The Rails sandbox In development, Rails' sandbox mode might be useful. Testing and the migration codebase

When deploying a Rails application that is using Webpacker and Capistrano, there are a few configuration tweaks that optimize the experience. Using capistrano-rails capistrano-rails is a Gem that...

...adds Rails specifics to Capistrano, i.e. support for Bundler, assets, and migrations. While it is designed for Asset Pipeline (Sprockets) assets, it can easily be configured for Webpacker. This brings...

...changelog_path = File.expand_path('../CHANGELOG.md', __dir__) Ruby < 2.0 changelog_path = File.expand_path('../../CHANGELOG.md', __FILE__) Rails changelog_path = Rails.root.join('CHANGELOG.md...

You don't want sensitive user data in your logs. Background Rails per default filters sensitive data like passwords and tokens and writes [FILTERED] to the logs. The...

...code which is responsible for enabling that usually lives in filter_parameter_logging.rb (Rails.application.config.filter_parameters). Here is an example of a filtered log entry: Unfiltered: `User Load (0.4ms) SELECT "users".* FROM...

makandra dev

Getting CSS (and JS) live reloading to work in a esbuild / Rails project is a bit of a hassle, but the following seems to work decently well. We assume that...

...you already use a standard "esbuild in Rails" setup, and have an esbuild watcher running that picks up your source code in app/assets and compiles to public/assets; if not change...

...you UTC objects whose to_s(:db) may not convert properly. Legacy behavior in Rails 2.3 It's been briefly mentioned in the random list of ActiveSupport goodies, but please...

...remember to always use Time.current instead of Time.now, etc. Why? Because of the way Rails and MySQL deal with time zones you would need to take care to use Time.zone.now...

...up repetitive expectations in your specs. Unfortunately the default directory structure generated by rspec-rails has no obvious place to put custom matchers or other support code. I recommend storing...

...to all specs, put the following into your spec_helper.rb, above the RSpec.configure block: Dir[Rails.root.join("spec/support/**/*.rb")].sort.each {|f| require f} Also see where to put shared example groups...

...the same object. You also know that you can reload an association to make Rails load its data from the database again. user.posts.reload # discards cache and reloads and returns user.posts...

...reset returns the association/scope. Hence, the above will not seem to work on the Rails console, just because the return value is inspected and thus resolved right away.

if 'foo' =~ /foo/ puts $LAST_MATCH_INFO[1] # => foo end Require pitfall in Rails The English library is not loaded by default in Rails. So you or another library...

By default, Rails' validates_uniqueness_of does not consider "username" and "USERNAME" to be a collision. If you use MySQL this will lead to issues, since string comparisons are case...

...may fail with an SQL error due to duplicate index key. You can change Rails' behaviour, by saying class User < ActiveRecord::Base validates_uniqueness_of :name, case_sensitive: false

makandra Curriculum

Basic validations Read the Rails Guide on ActiveRecord Validations. You should have an overview which kinds of validations are built into Rails. Also read Testing ActiveRecord validations with RSpec.

...colored red. In addition, an invalid field control should have a red border. Tip Rails adds an extra element around invalid inputs to help with styling. Inspect an invalid field...

...few examples, where you configure some library via a block. One example is the Rails configuration: Rails.application.configure do |config| config.enable_reloading = false end This card describes a simple example on...

...You can use ActiveSupport::Configurable instead of the Configuration class. When you are using Rails with Zeitwerk and the code for e.g. FooClient lives in a folder, that is loaded...

makandra dev

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

makandra Curriculum

...piece, but it should be tidy and clear. If you have generated styles from Rails scaffolding, delete them all. They don't look nice and we do not use them...

...remove it and build your own styles from scratch. Tip Up until version 6, Rails understands Sass out of the box. No need to install additional software. Your MovieDB uses...

end html end Full disclosure: Under the hood this uses the private Rails helper method split_paragraphs that simple_format uses. While it might break when upgrading Rails...

Rails Active Support provides some helpful methods for calculating times and dates, like Duration#ago or Duration#from_now. But beware when using those, because they wont give...

...timezone unaware. Moreover, you have to be aware that ActiveSupport::TimeWithZone does not use Rails.application.config.active_record.default_timezone, which you need to define, even if you only use your local timezone, but...

...BEM, block-by-block. Read Read the chapter "Taming Stylesheets" from our book Growing Rails Applications in Practice (in our library). Talk with a colleague about the reasons for the...

...a good BEM structure in the DOM tree of these websites: https://makandra.de/ https://railslts.com/ https://www.aitiraum.de/ Practice: Cards In a new project, try to layout the style of...