Recent notes

RSS feed
June 17, 2013
1 thank

Use dom_id( resource_instance ) to create the HTML id

In the notes on this page people use:


But you can use this function in stead:

June 5, 2013 - (v2.0.3 - v3.2.13)
0 thanks

Html inside Lable tag

I need this

   <select size="1" name="dyntable_length" aria-controls="dyntable">
     <option value="10" selected="selected">10</option>
     <option value="25">25</option>
     <option value="50">50</option>
     <option value="100">100</option>


I made a helper method:

def entries_lablel()
   label_tag '' do
     concat 'Show '
     concat content_tag(:select, options_for_select([10, 25, 50, 100]),
       {name: 'dyntable_length', size: 1}
     concat ' entries'

and In my html.erb file I called it

<%= entries_lablel %>

You can pass paramateres to make it more generic also You can add multiple select elements or any other element using the same

June 3, 2013 - (1.3.0 - 1.3.1)
1 thank

as_null_object working

It only listen for the messages we tell it to expect and ignore any other messages.

For example:


module Codebreaker
 describe Game do
   describe "#start" do
     before(:each) do
       @output = double('output').as_null_object
       @game = Game.new(@output)

     it "sends a welcome message" do
       @output.should_receive(:puts).with('Welcome to Codebreaker!')
     it "prompts for the first guess" do
       @output.should_receive(:puts).with('Enter Guess:')

In first example we are expecting ‘Welcone to Codebreaker!’ while in second example we expect ‘Enter Guess:’

and in before(:each) first line we are using as_null_object which allowing us to only check if expected string exists in game.start method then it will pass.


module Codebreaker
 class Game
   def initialize(output)
     @output = output
   def start
     @output.puts 'Welcome to Codebreaker!'
     @output.puts 'Enter Guess:'
May 31, 2013
2 thanks

Do not mistakenly use serialize like other similar directives - attr_accessible, attr_accessor

serialize seems very similar to other directives that work on attributes such as attr_accessible. One may mistakenly assume that serialize can take a list of attributes. For eg:

class Tuk < ActiveRecord::Base
  attr_accessible :foo, :bar
  serialize :foo, :bar

This may lead to a cryptic error. Eg.

puts !Tuk.first.foo.nil?


NoMethodError at /file:location undefined method `new' for :bar:Symbol

This is because it tries to parse the YAML string stored in foo as an instance of :bar.

May 30, 2013
1 thank

Avoiding to_param method when using URL helper methods

I recently found myself in the situation where I needed to generate URLs which included the ID instead of the value returned from the model’s to_param method (since someone had overridden the to_param method). It turned out to be easier than I thought. You can simply pass an ID to the helper method and it will construct the URL correctly:

# /admin/foobars/123/edit
May 23, 2013 - (v3.0.0 - v3.2.13)
0 thanks

Use @output_buffer to set the context.

You can use assert_select to test helpers, just have to set the @output_buffer before you do.

Code example

class CurrencyHelperTest < ActionView::TestCase

  setup do
    # can use helper methods here
    @output_buffer = currency 54.78

  test 'currency use a div' do
    asert_select 'div'

May 23, 2013
0 thanks

A simple usage example

See http://apidock.com/rails/String/inquiry

env = "production".inquiry
env.production?  # => true
env.development? # => false
May 18, 2013
0 thanks

:include is also valid option

my_company.serializable_hash(:include => [:people])

May 16, 2013
0 thanks

Bug in Ruby or this documentation

%Q doesn’t return microseconds but milliseconds! Use %s%6N for microseconds.

May 16, 2013
1 thank

Bug in Ruby or this documentation

%Q doesn’t return microseconds but milliseconds! Use %s%6N for microseconds.

May 15, 2013
0 thanks
April 27, 2013 - (v3.1.0 - v3.2.13)
4 thanks
April 17, 2013
0 thanks


What is the purpose of

Hash[one: 1, two: 1]

When you can write

{one: 1, two: 2}

Aren’t you just passing a hash into the [] method?

April 10, 2013 - (v3.2.1 - v3.2.13)
2 thanks

be aware that this writes to tmp/cache

Its supposed to be http caching, but Rails will actually cache the response to whatever you specified as the cache store, *as well*, but only if you specify :public => true. The default is filestore so it will try to write to tmp/cache.

Only a problem if you don’t have the proper permissions set, in that scenario your apache/nginx logs could fill up very quickly with “permission denied errors”

Full explanation is here http://blog.tonycode.com/archives/418

April 4, 2013 - (>= v1.0.0)
0 thanks

increment_by_sql for PG

Note, if you’re using the code below for incrementing by SQL with a Postgres database, it’s not going to like the backticks. Just remove them:

def increment_with_sql!(attribute, by = 1)
  raise ArgumentError("Invalid attribute: #{attribute}") unless attribute_names.include?(attribute.to_s)
  original_value_sql = "CASE WHEN #{attribute} IS NULL THEN 0 ELSE #{attribute} END"
  self.class.update_all("#{attribute} = #{original_value_sql} + #{by.to_i}", "id = #{id}")
April 4, 2013
0 thanks

HTTPS request

Hey, guys!

You have one mistake in example code.

uri = URI('https://secure.example.com/some_path?query=string')

Net::HTTP.start(uri.host, uri.port,
  :use_ssl => uri.scheme == 'https').start do |http|
  request = Net::HTTP::Get.new uri.request_uri

  response = http.request request

Here HTTP::start method called twice. This code should look like

Net::HTTP.start(uri.host, uri.port,
  :use_ssl => uri.scheme == 'https') do |http|
  request = Net::HTTP::Get.new uri.request_uri

  response = http.request request

It’s work - I checked.

April 2, 2013 - (v1_8_6_287 - v1_9_3_392)
0 thanks

Passing in an Array instead of individual arguments

Pass in array instead of list

h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }

keys_i_want = %w(cow cat)

h.values_at(*keys_i_want) #=> ["bovine", "feline"]
April 2, 2013 - (v3.0.0 - v3.2.13)
0 thanks

Use concat insted of joining collection explicitely

concat method will be useful to join the collection object from looping conditions.

arr = ["a", "b", "c"]
content_tag(:ul, :class => 'a class') do
  arr.each do |item|
  concat content_tag(:li, item)

And this will generate the html as shown below

<ul class="a class">
April 2, 2013 - (v1_9_3_392)
0 thanks

output GBK

‘I am 中国人’.encode(‘gbk’,‘utf-8’)

April 2, 2013
0 thanks

you need kconv

require ‘kconv’



March 29, 2013
0 thanks
March 29, 2013
0 thanks

Edge case

Have look how #between? handle adge case. It’s different from DateTime’s.

Date.yesterday.between?(Date.yesterday, Date.tomorrow)
=> true

Date.tomorrow.between?(Date.yesterday, Date.tomorrow)
=> true
March 27, 2013 - (>= v3.0.0)
0 thanks

When dealing with has_many through

The non-repeating primary key id must be used with find_in_batches.

  • User has many things

  • User has many socks through things

  • Sock has many things

  • Sock has many users through things

For the sake of argument, assume the first user has two socks and all other users have one sock. There are 1000 users in total and 1001 socks in total.

=> 1001
agg = []
# Incorrect
User.joins(:socks).find_in_batches{|g| agg += g}
=> 1000

=> 1001
agg = []
# Correct
Sock.joins(:users).find_in_batches{|g| agg += g}
=> 1001
March 27, 2013 - (v3.0.0 - v3.2.13)
0 thanks
March 18, 2013
4 thanks

Beware - May cause performance issues

A serialized attribute will always be updated during save, even if it was not changed. (A rails 3 commit explains why: http://github.com/rails/rails/issues/8328#issuecomment-10756812)

Guard save calls with a changed? check to prevent issues.

class Product < ActiveRecord::Base
  serialize :product_data


product = Product.first


product = Product.first
product.save if product.changed?
March 13, 2013
1 thank

Another Hash#without

Mange made me think, and I wanted to expand on his example with a small change.

class Hash
  def without(*keys)

  def without!(*keys)
    reject! { |key| keys.include?(key) }

h = { :a => 1, :b => 2, :c => 3 }
h.without(:a)      #=> { :b => 2, :c => 3 }
h #=> { :a => 1, :b => 2, :c => 3 }  

h.without(:a, :c)  #=> { :b => 2 }

h.without!(:a, :c) # { :b => 2 }
h #=> { :b => 2 }
March 5, 2013
0 thanks

Minor correction to Rubybull's examples?

Was your first example intended to be:

=> [11, 22, 31, 224, 44]
a.each.with_index { |val,index| puts "index: #{index} for #{val}" }
March 5, 2013 - (v3.0.0 - v3.2.8)
0 thanks

Exceptions raised within are ignored.

From http://guides.rubyonrails.org/active_record_validations_callbacks.html

The after_commit and after_rollback callbacks are guaranteed to be called for all models created, updated, or destroyed within a transaction block. If any exceptions are raised within one of these callbacks, they will be ignored so that they don’t interfere with the other callbacks. As such, if your callback code could raise an exception, you’ll need to rescue it and handle it appropriately within the callback.

March 1, 2013
0 thanks

Default field order

If you want to set an app wide default order for the fields (rather than passing :order each time), use the locale file.

eg. edit config/locale/en.yml to include:

      - :day
      - :month
      - :year