Be careful with Array(...)
A popular ruby idiom I keep stumbling upon is
def do_some_thing_for(values)
values = Array(values)
values.each { ... }
end
The intent is to allow the user to pass in arrays as well as scalar values, since
Array("foo") == ["foo"]
Array(["foo", "bar"]) == ["foo", "bar"]
But Array()
actually calls to_a
on its arguments, which may not always do what you expect. For example
Array("foo \n bar") == ["foo \n", "bar"]
Related cards:
Be very careful with 301 and 308 redirects
Browsers support different types of redirects.
Be very careful with these status codes:
301 Moved Permanently
308 Permanent Redirect
Most browsers s...
PostgreSQL: Be careful when creating records with specific ids
In tests, it is sometimes useful to create records with specific ids. On PostgreSQL this can cause problems:
Usually, PostgreSQL uses an "autoincrement" sequences to provide sequential ids for new database rows. However, these sequences will not ...
PSA: Be super careful with complex `eager_load` or `includes` queries
TLDR
Using
.includes
or.eager_load
with 1-n associations is dangerous. Always use.preload
instead.
Consider the following ActiveRecord query:
BlogPost.eager_load(
:comments
:attachments,
).to_a
(Let's assume...
Careful when calling a Ruby block with an array
When a Ruby block or proc takes multiple parameters, and you call it with an Array
, Ruby will unexpectedly splat the array elements:
block = proc { |a, b| "a=#{a}, b=#{b}" }
block.call(1, 2) # "a=1, b=2"
block.call([1, 2]) # "a=1,...
Rails 3 routing: Be careful with matching routes *including their format*
Today, this line made me trouble. Can you spot the mistake?
match 'sitemap.xml' => 'feeds#sitemap', :constraints => { :format => 'xml' }, :as => 'sitemap'
The mistake is to match sitemap.xml
. Rails will by default strip any dot-anythin...
Be careful with "memoize"
ActiveSupport
's memoize
has a dangerous feature you might not know about.
Assume you have
class DeepThought
extend ActiveSupport::Memoizable
def life_universe_and_everything
# some lengthy calculation returning 4...
PostgreSQL: How to add/remove/modify array values (and how to replace 1 value with multiple values)
PostgreSQL's array data type is pretty useful, but manipulating values of arrays can be awkward because of its syntax.
Consider the following users
table which each example below will sta...
ActiveType::Object: Be careful when overriding the initialize method
Background:
ActiveType::Object
inherits from ActiveRecod::Base
and is designed to behave like an ActiveRecord Object, just without the database persistence.
Don't remove any of the default behavior of the initialize method!
If you hav...
RSpec < 2.11: ActiveRecord scopes must be loaded before using the "=~" matcher
To test whether two arrays have the same elements regardless of order, you can use the =~
matcher in RSpec < 2.11:
actual_array.should =~ expected_array
If either side is an ActiveRecord scope rather than an array, you should ca...
Be careful to use correct HTTP status codes for maintenance pages
When your public-facing application has a longer downtime for server maintenance or long migrations, it's nice to setup a maintenance page to inform your users.
When delivering the maintenance page, be *...