...ActiveRecord::Base.establish_connection(config) end it 'should synchronize processes on the same lock' do (1..20).each do |i| fork_with_new_connection do @reader.close ActiveRecord::Base.connection.reconnect! Lock.acquire('lock') do...
...a typo or similar lines.each_slice(2) do |start, finish| start.should =~ /Started: (.*)/ start_thread = $1 finish.should =~ /Finished: (.*)/ finish_thread = $1 finish_thread.should == start_thread end @reader.close end end Caveats
...approaches in Rails, how you can assign such an association via HTML forms. Option 1: Array column One way to solve the problem is using a database column with an...
...We need to take care of not assigning nil as value of posts Option 1: Using with_defaults! in the controller + include_hidden: false in the form (used in the...
...column_value(value) method in active record is traced (simplified output): git log -n 1 --pretty=oneline -S 'convert_number_column_value(value)' -- activerecord/lib/active_record/base.rb ceb33f84933639d3b61aac62e5e71fd087ab65ed Split out most of the...
...activerecord/lib/active_record/base.rb - def convert_number_column_value(value) - if value == false - 0 - elsif value == true - 1 - elsif value.is_a?(String) && value.blank? - nil - else - value - end - end activerecord/lib/active_record/attribute_methods/write.rb + def convert_number_column...
...the constructor. returnValue & returnValues it('shuffles the array', () => { spyOn(Random, 'shuffle').and.returnValue([3, 2, 1]) array = [1, 2, 3] testedClass = new testedClass(array) expect(Random.shuffle).toHaveBeenCalled() expect(testedClass.array).toEqual...
}) If you have several new classes created in one test you could also use returnValues for any call of the method Random.shuffle. callFake If you are testing the class...
...can list them like this: $ git stash list stash@{0}: WIP on feature/foo stash@{1}: WIP on feature/bar stash@{2}: WIP on fix/baz All those stashed changes have their own...
...reference (like branch names) that you can look at, like stash@{0}, stash@{1}, etc. Above, we looked at stash@{0} which is the default for stash actions.
...used for the same effect. Relation#to_sql # Rails 2 scope Post.scoped(:conditions => { :id => [1, 2] }).to_sql # => "SELECT `posts`.* FROM `posts` WHERE `posts.id` IN (1, 2)" ^ # Rails 3 relation...
...Post.where(:id => [1, 2]).to_sql # => "SELECT `posts`.* FROM `posts` WHERE `posts.id` IN (1, 2)" Implementation note: Rails 3+ implements #to_sql. Relation#to_id_query Site.joins(:user).where(:users...
...A-Za-z0-9]+> is easier to write but matches invalid tags such as <1>. Use \b[1-9][0-9]{3}\b to match a number between 1000 and...
...Use \b[1-9][0-9]{2,4}\b matches a number between 100 and 99999. Modes: Greedy, Lazy and Possessive Example string: This test is a first test string...
# for which the block evaluates to true, # the second containing the rest. (1..6).partition { |v| n.even? } #=> [[2, 4, 6], [1, 3, 5]] Works well with destructuring assignment...
even, odd = (1..6).partition { |n| n.even? } even #=> [2, 4, 6]
attribute attr end validates *REQUIRED_ATTRIBUTES, presence: true validates *ALL_ATTRIBUTES, length: { maximum: 1000 } # From this you already get methods like `#valid?` and `#==`. # (optional) Here is the perfect place...
...Address instance: # without :mapping Rails uses keyword arguments Address.new(street_and_number: 'Foo St. 1', zip_code: '12345', city: 'Bar Town') # with a :mapping (even if that is just the...
The git doc states on the difference of these two commands: git-restore[1] is about restoring files in the working tree from either the index or another commit. This...
...also be used to restore files in the index from another commit. git-reset[1] is about updating your branch, moving the tip in order to add or remove commits...
...This task list is divided by the Gate keeping process in the following steps: 1. Starting a new feature 2. Working on the issue 3. Finishing a feature
...design and the points from Building web applications: Beyond the happy path The template: 1 Starting a new feature Set the issue to "In Progress" in Linear Pull the most...
...a new frame is rendered 60 times a second. Thus, a single frame is 1000ms / 60 = 16.6...ms long. Once a frame renders longer than 16ms, the page will begin...
...s well searchable when you just have a question about some function. Performance issue 1: Slow JavaScript If an event occurs and JavaScript has to run, the browser have to...
...t: the current time in 24-hour HH:MM:SS format, \T: same, but 12-hour format, @: same, but in 12-hour am/pm format \n: newline \r: carriage return
...x: attribute of the text 3y: foreground color 4y: background color x: 0: normal 1: bold 4: underline 7: reverse y is the color: 0 black 1 red
The sprintf method has a reference by name format option: sprintf("% d : % f", { :foo => 1, :bar => 2 }) # => 1 : 2.000000 sprintf("%{foo}f", { :foo => 1 }) # => "1f" The format identifier % stands for...
...different data types to be formatted, such as %f for floats: sprintf('%f', 1) # => 1.000000 Example: This is quite useful to replace long strings such as API endpoints which might...
2 application servers 6 Rails worker processes in each application server 1 background job server with Sidekiq (will spawn 10 threads by default) When your code does...
...you may use the following number of connections: (2 app servers * 6 app workers) + (1 job server * 10 sidekiq workers) = 12 + 10 = 22 connections When your code does use ActiveRecord...
...database's EXPLAIN style. For example, it looks like this on MySQL: User.where(id: 1).includes(:articles).explain EXPLAIN for: SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 +----+-------------+-------+-------+---------------+
...type | table | type | possible_keys | +----+-------------+-------+-------+---------------+ | 1 | SIMPLE | users | const | PRIMARY | +----+-------------+-------+-------+---------------+ +---------+---------+-------+------+-------+ | key | key_len | ref | rows | Extra | +---------+---------+-------+------+-------+ | PRIMARY | 4 | const | 1 | | +---------+---------+-------+------+-------+ 1 row in set (0.00 sec) EXPLAIN for: SELECT `articles...
...has_many :users end Here activity 42 has four users: activity = Activity.find(42) activity.users.ids # => [1, 2, 3, 4] Let's say we want to do the same on all activities...
...By reloading the object, its full list of associated users is restored: activity.reload.users.ids # => [1, 2, 3, 4] Or you can reset the association cache: activity.users.reset # newer Rails activity.users(true) # old...
Unfiltered: `User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."token" = $1 LIMIT $2 [["token", "secret-token"], ["LIMIT", 1]]` After the filter is applied:
...ms) SELECT "users".* FROM "users" WHERE "users"."token" = $1 LIMIT $2 [["token", "[FILTERED]"], ["LIMIT", 1]]` Unfortunately, this mechanism is not always applied, because of code dependencies. Some are written by...
...using Geordi, disable automatic updating of chromedriver in ~/.config/geordi/global.yml: auto_update_chromedriver: false Option 1: Use Geordi The geordi gem can upgrade to the correct version of chromedriver: geordi chromedriver...
...z "$VERSION" ]; then echo "Failed to read current version from $VERSION_URL. Aborting." exit 1 else echo "Current version is $VERSION" fi # Abort script if any of the next commands...
...a SHA1 revision. Your editor will open with a file like pick fda59df commit 1 pick x536897 commit 2 pick c01a668 commit 3 Each line represents a commit (in chronological...
...these commits into a single one, change the file to this: pick fda59df commit 1 squash x536897 commit 2 squash c01a668 commit 3 This means, you take the first commit...
...enabled for node. Installing nvm DigitalOcean has a HOWTO for installing nvm on Ubuntu (16.04, 18.04, 20.04) that I recommend. During the installation of node via nvm, a compatible version...
...to be done separatly for each node version on your system, though. Install yarn 1 system-wide via apt The yarn package depends on the nodejs debian package, but with...
...within the diff. Colors The value for color configuration variables is a list of 1-2 colors and 0-1 attributes, separated by spaces. Colors: normal, black, red, green, yellow...
end def db_number # Returns a number between 2 and n. # Redis db#1 is used for development. db_number = 1 if rails_env == 'test' normalized_test_number = [ENV...
...TEST_ENV_NUMBER'].to_i, 1].max db_number += normalized_test_number end db_number end def port case rails_env when 'staging' # when 'production' # else 6379 # default Redis port...
...is to encode the string using JSON, and then escaping all code points above 127 using unicode escape sequences: escapeHighASCII('{"foo":"xäy"}') // => '{"foo":"x\\u00e4y"}' Escaping high ASCII in JavaScript...
...function escapeHighASCII(string) { let unicodeEscape = (char) => "\\u" + char.charCodeAt(0).toString(16).padStart(4, '0') return string.replace(/[^\x00-\x7F]/g, unicodeEscape) } Escaping high ASCII in Ruby def escape_high_ascii(string...