api.rubyonrails.org

...class Image < ActiveRecord::Base; end class Video < ActiveRecord::Base; end class PageVersion < ActiveRecord::Base belongs_to :primary_medium, polymorphic: true # may be Image or Video end class Page < ActiveRecord::Base...

...belongs_to :current_version, class_name: 'PageVersion' end Now, when you load pages and include their current versions' primary media... Page.includes(:current_version => :primary_medium).to_a ... Rails will produce...

Webpacker uses Babel and Webpack to transpile modern JavaScript down to EcmaScript 5. Depending on what browser a project needs...

stackoverflow.com

This card will show you how to use git rebase --onto without confusion. Use case You've got two feature...

Fill in find field with e.g. url (? .*?) Hint: the "url" at the beginning of the string is just a text fragment; the (? .*?) is our named capture group.

In an environment: config.logger = Logger.new('/dev/null')

...to e.g. lock an entire form while it is being changed. This requires code beyond database locking, in both backend and frontend...

Looking at the source code of the validates_numericality_of validator, it becomes clear that it converts the attribute in question to either an integer or float: if configuration[:only...

...ActiveRecord::Errors.default_error_messages[:not_a_number]) next end raw_value = raw_value.to_i else begin raw_value = Kernel.Float(raw_value.to_s) rescue ArgumentError, TypeError record.errors.add(attr_name, configuration[:message] || ActiveRecord::Errors.default...

This is painful. Consider using Microsoft Office or switching careers. If you need to write < 20 letters consider doing it...

TL;DR There are three dimensions you can control when scoping routes: path helpers, URL segments, and controller/view module.

When ending a Selenium test Capybara resets the browser state by closing the tab, clearing cookies, localStorage, etc.

If you want to collapse/expand elements with dynamic content (and thus unknown height), you can not transition between height: 0...

...skipping asset compilation when assets have not changed? Turns out there is an even better way to speed up Capistrano deployments with asset compilation – and it's even simpler.

relishapp.com

RSpec 3 has verifying doubles. This breed of mock objects check that any methods being stubbed are present on an...

ruby-doc.org

...read and write access by methods like File.open and File.read. def exclusively_locked_access begin @file_locked = true File.open(F"#{file_path}.lock"), 'w+') do |f| f.flock(File::LOCK_EX...

...that block make sure to do that manually and preferably wrap it it with begin and rescue if something might go wrong: begin f = File.open(file, File::CREAT) f.flock(File...

When you want to group rails models of a logical context, namespaces are your friend. However, if you have a...

While verifying doubles in RSpec is a good default, it is limited in the amount of methods it actually is...

...all Unpoly updates work using AJAX requests, you won't get the more detailled better_errors page with the interactive REPL. Below is an event listener that automatically repeats the...

...full CSS and JavaScript. The code assumes you are using Ruby on Rails with better_errors, which is the default error view that modern Rails versions employ. If you are...

Debugging your integration tests, that run a headless Chrome inside a docker image, is tricky. In many cases you can...

In /etc/rc.local add: service bluetooth stop

makandra dev

...installation supports YJIT, by running: $ ruby --yjit -v ruby 3.3.4 (2024-07-09 revision be1089c8ec) +YJIT [x86_64-linux] If YJIT is not available, the output of this command will...

...code comments come into play: Use code comments to document Hows and Whys Group belonging lines with empty lines ("code paragraphs") New code is usually bright and clear to the...

blog.saeloun.com

Rails' fragment caching caches subtrees of an HTML document tree. While constructing that tree though, it can be really hard...

makandra dev
github.com

The rack-contrib gem brings a JSONP middleware that just works™. Whenever a JSON request has a callback parameter, it...

makandra dev
gist.github.com

When ActiveRecord child objects are autosaved in Rails.