Within development and test environments, Rails is usually configured to show a detailed debug page instead of 404s. However, there might be some cases where you expect a 404 and...
...be used as a light-weight version of integration tests here.) In this case, Rails will replace the 404 page that you want to test for with its debug page...
If validations failed for a record, and you want to find out if a specific validation failed, you can leverage...
Postgres supports multiple built-in range datatypes: int4range int8range numrange tsrange (range with timestamp without timezone) tstzrange (range with timestamp...
...more than changing tables (like, modify many records) you may want some output. In Rails > 3.1 you have two methods at hand: announce and say_with_time. In the migration...
In Rails, we usually have a mailer setup like this: class MyMailer < ActionMailer::Base def newsletter mail to: 'receiver@host.tld', from: 'sender@host.tld', subject: 'My mail' end end If you want to...
mail = MyMailer.newsletter File.open('my_mail.eml', 'w') { |file| file.write(mail.to_s) } Now, close the rails console and preview the mail: xdg-open my_mail.eml There is not much magic in this...
Large Rails projects tend to define multiple custom ways to format Dates or DateTimes. This often leads to duplicated format strings, wrong formats and unnecessary introduction of new formats.
In Rails 5+ you can access a helper from a controller using the helpers method: # Inside a controller action helpers.link_to 'Foo', foo_path In older Rails versions you can...
...an instance of its subclasses or superclass. This is useful because some parts of Rails reflect on the class of an instance, e.g. to decide which partial to render:
Just like Ruby Gems tag their version releases to the corresponding Git commit, it can be helpful to track production...
Look out for a number that keeps growing over time. Rails In a Rails application, you can use derailed_benchmarks to identify and examine memory leaks.
Webpacker is Rails' way of integrating Webpack, and version 4 has been released just a few days ago, allowing us to use Webpack 4. I successfully upgraded an existing real...
...are notes on everything that I encountered. Note that we prefer not using the Rails asset pipeline at all and serving all assets through Webpack for the sake of consistency...
...partial_double_verification. This might be handy in case you are testing helpers in Rails, where you sometimes rely on methods defined in the application controller (e.g. current_user and...
You can use constraints in your routes.rb to avoid getting ActionView::MissingTemplate errors when wrong routes are called. Instead, the...
...use SimpleForm in your project, I would suggest to regenerate the configuration file with rails generate simple_form:install --bootstrap and merge it with your own customizations instead of only...
Looks simpler than inaction_mailer: gem install mailcatcher mailcatcher Setup Rails to send mails to 127.0.0.1:1025. Usually you want the following config in config/environments/development.rb and maybe in test.rb or...
Note: Instead of using the method in this card, you probably want to use ActiveType's nested attributes which is...
The :test adapter doesn't respect limits_concurrency configuration. Switch to :solid_queue adapter in your test to verify blocking...
...pass a :status option: get '/stories', to: redirect('/articles', status: 302) Note By default Rails sends a header Cache-Control: max-age=0, private, must-revalidate with all responses, including...
Rails 6.1 has a "strict loading" mode that forces the developer to preload any association they plan to use. Associations no longer load lazily. An error is raised when reading...
...some cases, a viable workaround is to turn your images into inline attachments. Note Rails provides a simple mechanism to achieve this: https://guides.rubyonrails.org/action_mailer_basics.html#making-inline-attachments This documentation makes it look...
...It is recommended to watch for a feature flag, see below. return version unless Rails.config.feature_inline_email_images # `attachments` is provided by Rails, see the linked documentation above # URLs should...
The linked article suggests an interesting way to speed up tests of Rails + Postgres apps: PostgreSQL allows the creation of “unlogged” tables, which do not record data in the PostgreSQL...
...the project might need to re-recreate their test databases like so: bundle exec rails parallel:drop && bundle exec rails parallel:prepare
...into a database console, run SET GLOBAL query_cache_type=OFF; and restart your rails server. You can also disable the cache on a per query basis by saying
...SQL_NO_CACHE * FROM ... You also probably want to disable Rails internal (per-request) cache. For this, wrap your code with a call to ActiveRecord::Base.uncached. For example, as an...
...want to find all Decks without any Card or all Cards without a Deck. Rails 6.1+ Rails 6.1 introduced a handy method ActiveRecord#missing to find records without given associations...
ON "decks"."id" = "cards"."deck_id" WHERE "decks"."id" IS NULL Older Rails versions If you don't have Rails 6.1 yet, you can use this not-so...
Since Rails 6+ you can use before? and after? to check if a date/time is before or after another date/time. Example christmas = Date.parse('24.12.2022') date_of_buying_a...