By activating strict_loading you force developers to address n+1 queries by preloading all associations used in the index...
...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...
Adopting legacy Rails apps Talk to your mentor about how we're approaching applications that are either old or abandoned by a different team earlier: Add E2E tests for 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...
...insert many records is to have a single INSERT statement describing multiple rows. In Rails 6+ you can do so with ActiveRecord::Base.insert_all. This is very fast, but you...
...Fixtures are handy for development seed data, they can be loaded in development with: rails db:fixtures:load Downsides Less matchers & library support It is harder to mock with minitest...
...twentieth in what ordering? The ordering is unknown, unless you specified ORDER BY. In Rails, if you use Record.first or Record.last, it will default to ordering by id.
...Best practices for writing code comments Read the following chapters from our book Growing Rails Application in Practice: Dealing with fat models Extracting service objects Discuss with your mentor what...
The change_column method for rails migrations support casting with a custom SQL statement. This allows us to change a column type and keep the former content as the new...
You can report CSP violations to Sentry. Within config/initializers/content_security_policy.rb: Rails.application.configure do config.content_security_policy do |policy| # Settings for the policy policy.report_uri 'https://ooo4444bbb.ingest.de.sentry.io/api/ooo4444bbb/security/?sentry_key=ooo4444bbb' end end Replace the actual...
Rails 6 includes a WYSIWYG editor, Action Text. It works out of the box quite well, but chances are that you want to add some custom functionality. This card contains...
...some tips how to achieve this. Setup Basically, follow the guide in the Rails documentation. The automated script may not work with the way webpacker is configured in your project...
When using Rails to truncate strings, you may end up with strings that are still too long for their container or are not as long as they could be. You...
} start your server as usual, but go to https://localhost:3000 bundle exec rails s Accept the certificate in your browser See also Creating a self-signed certificate for...
Since Rails 6.1+ you can use .compact_blank or .compact_blank! to remove blank values from collections (e.g. arrays). Remove nil values from an array ['foo', nil].compact...
Remove blank values from collections Array array = [1, "", nil, 2, " ", [], {}, false, true] # Any Rails version array.reject(&:blank?) # => [1, 2, true] # Since Rails 6.1+ array.compact_blank # => [1, 2, true]
...to some path methods generated by your routes. Even though you could technically include Rails.application.routes.url_helpers, this may include way too many methods and even overwrite some class methods in...
...advised to only make the desired methods available: class Project delegate :url_helpers, to: 'Rails.application.routes' def project_path url_helpers.project_path(self) end
position: relative left: -50% float: left .clear clear: both Together with this helper: # Rails 3 def center_float(&block) concat( content_tag(:div, :class => 'center_float_outer_container') do...
...tag(:div, :class => 'center_float', &block) end end + content_tag(:div, :class => 'clear') ) end # Rails 2 def center_float(&block) html = "".html_safe html << content_tag(:div, :class => 'center_float...
We recently migrated a Rails application from yarn to npm. We decided to go this step instead of upgrading to > Yarn 2.0 to reduce the number of dependencies in our...
...your yarn.lock (after the first npm install you can relax the constraints again). jsbundling-rails supports NPM since v1.2.2. geordi supports package managers other than yarn since v11.1.0...
This card describes how to install some tasks only for a given Rails environment or for a given Capistrano stage ("deployment target"). Installing jobs only for a given...
...Rails environment In your schedule.rb you may use environment variable to access the Rails environment of the current deployment: if environment == 'staging' every :day do # Setup job that will only...
By default most exceptions in Rails will render a 500 error page and will create a new issue in your error monitoring. There are some built-in rules in Rails...
Code snippet tested with Rails 2.3 def index # ... if request.xhr? html = render_to_string(:partial => "list", :layout => false) respond_to do |format| format.html { render :text => html } format.json { render :json => {:html...
...this card might help. If you call render_to_string within the format.json block, Rails will only look for an index.json template, but not for an index.erb template...
...allows you to log to multiple sinks. You know this behavior from from the rails server command, that both logs to standard out and the log/development.log file.
...development.log file. Here is an example for Sidekiq: Sidekiq.configure_client do |config| if ENV['RAILS_ENV'] == 'development' || ENV['RAILS_ENV'] == 'test' stdout_logger = ActiveSupport::Logger.new(STDOUT) file_logger = ActiveSupport::Logger.new...
To return non-HTML responses (like XLS spreadsheets), we usually use the respond_to do |format| format.xls do # send spreadsheet...
Sometimes, the rails dev server doesn't terminate properly. This can for example happen when the dev server runs in a RubyMine terminal. When this happens, the old dev server...
...note that some of the functions edge_rider provides have native implementations in newer rails versions. Useful in applications Relation#traverse_association(*names) Edge Rider gives your relations a method...
...These utilities are mostly useful in libraries that need to support multiple versions of Rails. They offer a unified interface across Rails versions. Relation#collect_ids You should not use...