An association defined with has_many :through will return the same record multiple times if multiple join models for the same record exist (a n:m relation). To prevent this...
invoice_id product_id 11 1001 55 12 1001 55 Now #products will return the product with ID #55 two times for this invoice: invoice = Invoice.find(1001) invoice.products # => [# , # ]
...IN (?)", excluded_ids) When the exclusion list is empty, you would expect this to return all records. However, this is not what happens: # Broken example User.where("id NOT IN (?)", []).to...
...SELECT `users`.* FROM `users` WHERE (id NOT IN (NULL)) Passing an empty exclusion list returns no records at all! See below for better implementations. Rails 4+ Use the .not method...
Use return to return from a method. return accepts a value that will be the return value of the method call. Use break to quit from a block and...
...serve as an example in the details below: def example puts yield puts 'done' return 'example' end # Intended usage & output: example { 'hallo welt' } # hallo welt # done # => 'example' Return within a...
...any event handlers attached through jQuery's live method, since those depend on bubbling. returning false in the event handler only exists for jQuery event handlers, but not for native...
... you probably have a time zone issue. When you get Timecop.travel(Date.parse("2011-11-11 00:00") do Time.current # Thu...
Use these snippets when you want to measure yourself. Currently available: Core Web Vitals Largest Contentful Paint (LCP) Largest Contentful...
SomeApi.should_receive(:find).with(:query => '*foo*', :sort => 'timestamp ASC', :limit => 100).and_return(['some result']) This is not very flexible, and failure messages will be hard to read...
In a nutshell: return statements inside blocks cause a method's return value to change. This is by design (and probably not even new to you, see below) -- but can...
...Just like you would expect. Now, consider this method: def proxy_thing stuff do return 42 end end Looks quite similar, right? Check this: >> proxy_thing yielding... => 42 Woah.
require 'spec_helper' describe Rational do describe '#to_s' do it 'should return correct results for actual rationals, as usual' do Rational(6, 4).to_s.should == '3/2' Rational...
...to_s.should == '1/11' Rational(-2, 3).to_s.should == '-2/3' end it 'should not return a rational representation of integer results, like Ruby 1.8 did' do Rational(2, 1).to_s.should == '2' Rational...
TLDR: A function is hard to use when it sometimes returns a promise and sometimes throws an exception. When writing an async function, prefer to signal failure by returning a...
...rejected promise. The full story When your function returns a promise ("async function"), try not to throw synchronous exceptions when encountering fatal errors. So avoid this: function foo(x) {
When submitting textareas, browsers sometimes include carriage returns (\r) instead of just line feeds (\n) at the end of each line. I don't know when this happens, and most...
In cases where it does matter, use the attached trait to remove carriage returns from one or more attributes like this: class Note does 'strip_carriage_returns', :prose, :code...
As you know, assignable_values does not invalidate a record even when an attribute value becomes unassignable. See this example...
...should_receive these stubbed methods may also yield blocks. This is handy if the returning object is receiving a block call. Consider this, where you cannot say and_return [] because...
...Message.find_in_batches do |messages| messages.each(&:crawl) end end It works similar to and_return -- just use and_yield: describe '#crawl_messages' do it 'should process messages in batches so...
...from your shell scripts, you might find it useful that a failed bundle call returns a different error code depending on the type of problem. A list of error codes...
In the ruby shell (IRB) and rails console the return value of the previous command is saved in _ (underscore). This might come in handy if you forgot to save the...
Since Ruby 2.1, defining a method returns its name as a Symbol: def foo() end # => :foo define_method :foo do end # => :foo You can use this to do Python-like...
MySQL's MIN and MAX functions are for aggregations only. This will not work and produce an error:
If you need to strip carriage return characters from a text file, you can use Vim: vim file.txt :set ff=unix
...our example these "Failed requests" actually never failed.\ For some requests, the application just returned a response with a different content length than the first response. This is indicated by...
A collection of code snippets which return a boolean value for a regex comparison. regexp.match?(string) # Recommended for Ruby >= 2.4 !!(string =~ regexp) # Recommended for older Rubies regexp === string !(regexp !~ string...
When you are working with jQuery selectors and collections, many times you want to know if the collection actually contains...
The difference is that return false; takes things a bit further in that it also prevents that event from propagating (or “bubbling up”) the DOM...
...Let's start with the easiest case, an Object or object literal. Here typeof returns "object", as expected: x = {} x.foo = 'bar' x.foo // => 'bar' typeof x // => 'object' However, you can also...
x.foo // => 'bar' typeof x // => 'function' Finally JavaScript has a caveat that typeof null returns 'object'. No developer would consider this behavior useful, since you cannot get or set properties...
...your test. My specs often have a function htmlFixtures() that parses the HTML and returns a reference to all created elements: let [list, item1, item2] = htmlFixtures(` item 1
...recognize elements and how they relate to each other. Referencing created elements The function returns an array-like value with all created elements, in the same order they appear in...