To do so, call clear_active_connections! before your thread terminates: Thread.new do begin User.first # first database access makes a new connection ensure ActiveRecord::Base.clear_active_connections! end

thepugautomatic.com

How a macro can dynamically define a method that can be overridden with super in the same class.

Programatically invoke Rails generators Require the generator, instantiate it and invoke it (because generators are Thor::Groups, you need...

Some pseudo-elements need to be addressed with vendor prefixes. E.g. ::selection is not supported by Firefox, you need to...

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

makandra is responsible for maintaining about 75 Ruby projects. These projects use a large number of different versions for Ruby...

Git commits should be very deliberate, and only contain changes that you really want to be in there. In order...

Say you wrap your index view in a form to apply different filters like pagination or a search query. On...

The change_column method for rails migrations support casting with a custom SQL statement. This allows us to change a...

Whenever you create a table from a database migration, remember to add updated_at and created_at timestamps to that...

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

If you want someone to be able to access your rails console, but don't want them to be able...

...why we should always use ===, never == Testing the type of a value Understand the benefit of LoDash's _.isEqual What the f*ck JavaScript? Understand that there is no way...

...a callback function which can write it to console.log. Understand why each code block behaves the way it does: Variant 1: Iterating with forEach's callback function const callbacks...

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

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

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

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

index 0000000..a51cbad --- /dev/null +++ b/app/models/apple.rb @@ -0,0 +1,9 @@ +class Apple < Fruit + belongs_to :juice, class_name: 'AppleJuice' + + attr_accessor :type + + def to_s + I18n.t(type, scope: 'fruits.apples...

index 0000000..a51cbad --- /dev/null +++ b/app/models/pear.rb @@ -0,0 +1,9 @@ +class Pear < Fruit + belongs_to :juice, class_name: 'PearJuice' + + attr_accessor :type + + def to_s + I18n.t(type, scope: 'fruits.pears...

...move the directory to its new location old_store_path = Pathname.new(old_store_dir) begin FileUtils.rmdir(old_store_path.parent) # cleanup empty directories afterwards rescue Errno::ENOTEMPTY => e # ignore exception for non-empty...

Sometimes we write plain SQL queries in migrations so we don't have to mock ActiveRecord classes. These two migrations...

...NonExistentClass".constantize # => NameError: uninitialized constant NonExistentClass Dynamic Programming In scenarios where your application’s behavior depends on dynamic constant resolution, safe_constantize may offer a more convenient control flow to...

makandra dev

All browsers implement an event named beforeunload. It is fired when the active window is closed and can be used to display an alert to warn the user about unsaved...

...To trigger the alert, you have to call preventDefault() on the event. Note The beforeunload event is only dispatched when the user navigation makes a full page load, or if...

...purpose of this card. Update it once a day (using whenever) Tip Consider the best place to put the new logic. Should it be an existing class or new classes...

...plain RSpec mocks ("stubs") to replace the HTTP request to the API with scripted behavior. Can you minimize the number of lines of code that now no longer run during...

makandra dev

If you want Sidekiq to be able to talk to Redis on staging and production servers, you need to add...