ActiveSupport's memoize has a dangerous feature you might not know about. Assume you have class DeepThought extend ActiveSupport::Memoizable...

require 'open-uri' File.open('/target/path/to/downloaded.file', "wb") do |file| file.write open('http://example.com/your.file').read end Basic Authentication

There are several options, but most of them are impractical. The best way is to use the :ruby filter:

After starting the Rails server in a freshly generated Rails 3.1 project you could see an error message such as...

Note: This applies specifically to MySQL. In PostgreSQL for example, this is not an issue. If you care about performance...

Clearance is a gem that provides authentication functionality (e.g. login, logout). This note explains, how the clearance login, logout and...

By default, Rails views escape HTML in any strings you insert. If you want to insert HTML verbatim, you need...

RSpec 1 (Rails 2) With the most recent spec_candy.rb helpers you can say: User.stub_any_instance(:foo => :bar) user = User.new...

stackoverflow.com

The solution in this card is based on a stack overflow post by Leventix. If you need to make request...

rubyinside.com

While 2.0 will include a number of syntax changes, new features and general improvements, mentioned below, it is anticipated to...

First keep in mind that :dependent => :destroy hooks into before_destroy. So when you use other before_destroy callbacks the...

The most common use case for Ruby's #collect is to call a method on each list element and collect...

The Rails logger will store its content in a buffer and write it into the file system every 1000 lines...

In order to monitor a Rake task using God your Rake file must write a file with its process ID...

RSpec lets you define the arguments with which you expect a method to be invoked: subject.should_receive(:say).with('hello...

Calling #gsub on a string that was previously marked as #html_safe will lead to unexpected behavior. E. g. backreferences...

github.com

Looks simpler than inaction_mailer: gem install mailcatcher mailcatcher Setup Rails to send mails to 127.0.0.1:1025. Usually you want...

Defining a custom to_json method for your classes can be useful. Do it properly or you will "randomly" get...

When you require the Ruby debugger to be available from your Rails console (e.g. you want to inspect a method...

When adding a new field to your model's database table, don't set any defaults in the database.

Oniguruma is an advanced regular expression engine for Ruby. Install Oniguruma with binary dependencies like this: sudo apt-get install...

makandra dev

Never use raise_error without specifying the Error you expect. expect { do_a_lot_of_complicated_stuff }.to raise_error...

The technique described in this card has an important caveat: The result of GROUP_CONCAT is truncated to the maximum...

You can use heredoc to avoid endlessly long lines of code that nobody can read. Heredoc strings preserve linebreaks and...