github.com

...TILs on many topics that might be interesting for most of us. (e.g. Ruby, Rails, Git, Unix..) Ruby Here is an excerpt of all the Ruby TILs that were new...

SQL end Usage example: insert_record 'users', name: 'joe', age: 15 Also see Rails: Talking to the database without instantiating ActiveRecord objects...

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

To ensure Spring is not running: bin/spring stop pkill -f spring To prevent Spring from starting again: export DISABLE_SPRING...

Let's say you have a form that you render a few times but you would like to customize your...

When deploying Rails applications you might have noticed that JS and CSS are not cached by all browsers. In order to force Apache to add expiry dates to its response...

...when I change a file? Changed stylesheets and javascripts will always be reloaded because Rails appends a screen.css?1234567 timestamp to the paths. Background images referred to from the CSS...

...in our tests for sequential test execution but not for parallel test execution. And Rails requires you to set the config.cache_classes = false if you are using Spring in tests...

...With our setup, this would raise the following error in cucumber-rails for parallel test executions due to some legacy database cleaner issue. WARNING: You have set Rails' config.cache_classes...

Most of our applications use CarrierWave for file uploads. CarrierWave has an integrated processing mechanism for different file versions with...

To read the Rails session from a Rack middleware, use env['rack.session']. It's an ActionDispatch::Request::Session object. class MyMiddlware def initialize(app) @app = app end def call(env...

...status, headers, body = @app.call(env) session = env['rack.session'] Rails.logger.info("Value of session['foo'] is: " + session['foo'].inspect) [status, headers, body] end end You may not be able to write to...

If your irb or rails console keeps randomly crashing and you can't figure out why then you can try to disable multi-line autocomplete...

Rails comes with grouped_collection_select that appears to be useful, but isn't. As an alternative, consider the flat_grouped_collection_select found below. It takes a third argument...

The benefit of the Rails asset pipeline is that it compiles your stylesheets and javascripts to a single file, respectively. However, the consequences are startling if you don't understand...

...gave every library its own "root" folder. First, add this line to your config/initializers/assets.rb: Rails.application.config.assets.paths += Dir["#{Rails.root}/vendor/asset-libs/*"].sort_by { |dir| -dir.size } This will tell Rails about the custom asset...

...nicer to read, but has horrible security implications in some versions of Ruby on Rails. Affected versions Version Affected? Remedy 2.3.18 yes Use chain_safely workaround 3.0.20 no

Use Rails LTS 3.2 with hardened configuration 4.0.x ??? ??? 4.2.3 no Example When you chain two scopes with hash conditions on the same attribute, the second scope will overwrite...

...several gems that help to you do that, like Sidekiq or Resque. With newer Rails you can also use ActiveJob as interface for a background processing library. See here for...

end and a features/support/active_job.rb with: # Jobs should be worked off immediately in tests Rails.application.config.active_job.queue_adapter = :inline

dev.37signals.com

The author describes his little journey in hunting down a memory leak. Maybe his approach and tooling may one day...

...a situation in which I received the yarn integrity check warning when starting the rails console even though everything was up to date and correct versions in use.

I tried starting the rails console without switching to the correct node version first and received the yarn integrity warning. warning Integrity check: System parameters don't match...

paweldabrowski.com

The linked content includes a few design patterns implemented with Ruby on Rails. What is the card indented to achieve? You can use the pattern names for code reviews, so...

There is a way to use multiple databases in Rails. You may have asked yourself how you're able to keep your test databases clean, if you're running multiple...

...records to check if the migration works properly with those. after_initialize :readonly!, unless: -> { Rails.env.test? } end When defining multiple adapters in your database.yml, Rails needs to decide which one to...

stackoverflow.com

Create, or edit your ~/.irbrc file to include: require 'irb/ext/eval_history' # was 'irb/ext/save-history' for versions prior to Ruby 3.3 IRB.conf[:SAVE...

Returning an empty scope can come in handy, e.g. as a default object. In Rails 4 you can achieve this by calling none on your ActiveRecord model. MyModel.none # returns an...

...empty ActiveRecord::Relation object For older Rails versions you can use the attached initializer to get a none scope...

github.com

...eager-loading, and also if there is too much eager-loading. strict_loading in Rails 6.1+ forces developers to explicitly load associations on individual records, for a single association, for...

edgeapi.rubyonrails.org

Starting from Rails 4.0, you can use a special form options helper called #collection_check_boxes. It behaves similar to #collection_select, but instead of a single select field it...

Note the hidden input at the end. This is a trick that Rails uses so users can uncheck all the check boxes. If this hidden input wasn't...

...the standard configuration, though, it does not interpret the escape sequences used in the rails logfiles. To enable this type: less -R my_logfile.log You can also have an alias to...

After upgrading to Rails 6.1.7.2 one of our apps printed a wall of warnings while booting: /var/www/app/shared/bundle/ruby/2.6.0/gems/net-protocol-0.2.1/lib/net/protocol.rb:68: warning: already initialized constant Net::ProtocRetryError /home/deploy-app/.rbenv/versions/2.6.10/lib/ruby/2.6.0/net/protocol.rb:66: warning: previous definition of...