External content

MySQL 5.6 will expose tables as key/value stores through memcached, might be awesome

The next version of MySQL will include a built-in memcached daemon. This daemon can quickly get and set key/value pairs from and to InnoDB tables while completely bypassing the parsing and planning overhead of SQL.

This could potentially be very awesome because we would then have a distributed key/value store that stores data in a way it can also be queried with complex SQL queries.

Remove the module namespace of a qualified Ruby class name

You can use String#demodulize from ActiveSupport:

"ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
"Inflections".demodulize                                       # => "Inflections"

Market share of web browsers

The usage shares of your site highly depends on your target audience. E.g. no Internet Explorer has ever seen, but that's because of its tech-savvy audience. Distribution will also differ by other factors, such as region. See this Wikipedia article for details.

If you don't know your audience, you can use the stats of tracking tools, which see a lot of traffic go by a wide variety of sites. Some of these are:

  • [W3Counter](http://www.w3counter…

Updated: Remove quotes from Sass mixin arguments

When we looked at this card together a year ago, we were no longer sure if unquote is actually useful. I now found a good example for when you need unquote, and rewrote the card accordingly.

Maximum size of a MySQL query

Unless you changed the default, this will be 16 MB:

mysql> SHOW VARIABLES WHERE Variable_name="max_allowed_packet";
| Variable_name      | Value    |
| max_allowed_packet | 16777216 |
External content

Modifying Rake Tasks - Dan Manges's Blog

For custom Rake tasks, you shouldn't need to modify them after the original definition. However, if you want to add behavior to some vendor tasks (such as those defined with Rails), this blog post will cover how to do that.

Gem development: When your specs don't see dependencies from your Gemfile

When you develop a gem and you have a Gemfile in your project directory, you might be surprised that your gem dependencies aren't already required in your specs. Here is some info that should help you out:

  • Bundler actually doesn't automatically require anything. You need to call Bundler.require(:default, :your_custom_group1, ...) for that. The reason why you never had to write this line is that Rails does this for you when it boots the environment.
  • That also means that if you have an embedded Rails app in your spec folder (like [h…

Check if an object is an ActiveRecord scope

Don't say is_a?(ActiveRecord::NamedScope::Scope) because that is no longer true in Rails 3 and also doesn't match unscoped ActiveRecord classes themselves (which we consider scopes for all practical purposes).

A good way is to say this instead:


Updated: assignable_values can now humanize any given value, even if it's not the current attribute

You've always been able to access the humanized version for the current value like this:

song = => 'pop')
song.humanized_genre # => 'Pop music'

You can now also retrieve the humanized version of any given value by passing it as an argument:

song.humanized_genre('rock') # => 'Rock music'
External content

Using the Rake Build Language

Rake (like make) allows you to add dependencies to a task after you've
initially declared it. Indeed it allows you to continue to talk
about a task in multiple places. This way I can decide to add
dependencies close to the pre-requisite task

When connecting to a second database, take care not to overwrite existing connections

Sometimes, you may want to open up a second database connection, to a read slave or another database. When doing that, you must make sure you don't overwrite an existing connection.

The problem

While this may look good, it will actually cause all kinds of trouble:

def with_other_database

Putting aside that you are setting the general connection here (not generally a …

assignable_values 0.4.1 adds secondary default values

assignable_values now lets you define a secondary default that is only used if the primary default value is not assignable:

class Song < ActiveRecord::Base
  assignable_values_for :year, :default => 1999, :secondary_default => lambda { } do
    ( - 2) ..

If called in 2013 the code above will fall back to: # => 2013

This is especially useful in authorization scenarios with [Consul](https://github….

Consul 0.4.0 released

Consul 0.4.0 comes with some new features.


  • Consul no longer requires assignable_values, it's optional for when you want to use the authorize_values_for macro.
  • Consul no longer uses ActiveSupport::Memoizable because that's deprecated in newer Railses. Consul now uses Memoizer for this.

Temporarily change the current power

When you set Power.current to a power in an RS…

Use the "paper_trail" gem to track versions of records

paper_trail is an excellent gem to track record versions and changes.

You almost never want to reimplement something like it yourself. If you need to log some extra information, you can add them on top.

It comes with a really good README file that holds lots of examples. I'll show you only some of its features here:

  • Setting up a model to track changes
    Just add has_paper_trail to it:
    class User < ActiveRecord::Base
  • Accessing a previous version
    Saying user.previous_version gi…

How to change will_paginate's "per_page" in Cucumber features

The will_paginate gem will show a default of 30 records per page.\
If you want to test pagination in a Cucumber feature, you don't want to create 31 records just for that.

Instead, you probably want to modify the number if items shown, by saying something like this:

Given we paginate after 2 users

Using the following step definition, you now can! :)

Given /^we paginate after (\d+) (.*)$/ do |number, model_name|
  model = model_name.singularize.gsub(/[^A-z]+/, '_').camelize.constan...

When and Date.tomorrow return the same day...

… you probably have a time zone issue.

When you get"2011-11-11 00:00") do
  Time.current   => Thu, 10 Nov 2011 23:00:01 UTC +00:00       => Fri Nov 11 00:00:02 +0100 2011     => Fri, 11 Nov 2011
  Date.tomorrow  => Fri, 11 Nov 2011

you probably haven't defined a zime zone yet.

So might fix this by adding the following lines to your application.rb:

class Application < Rails::Application
  config.time_zone = 'Berlin' # or whatever your time zone
end   ...

MySQL operator precedence

Take care in queries where multiple AND or OR operators are used. In doubt, always use braces to enforce precedence.

3105 cards