Notes posted by edavey

RSS feed
October 30, 2008
1 thank

can be useful to achieve attr_reader effect


class Account < ActiveRecord::Base 

  def credit(amount)
    self.write_attribute(:balance, self.balance + amount)

  def balance=(value)
    raise "You can't set this attribute. It is private."


this allows fred.account.credit(5) whilst raising an error on fred.account.balance = 1000

August 10, 2008
4 thanks

converts a Time object to a string in rfc822 / rfc2822 format

>> time = p.updated_at

> Fri Jun 20 14:05:30 +0100 2008

>> time.class

> Time

>> time.httpdate

> “Fri, 20 Jun 2008 13:05:30 GMT”


July 23, 2008
3 thanks

options_for_select further example (using a collection and with a default value)

In this example, we are editing a collection of region records, each with its own select list of countries. (Region belongs_to :country.) If the region doesn’t have a country associated, then we want a default message of “unassigned”. Of course, if the region does have a country associated then we want that country displayed:

<% name = "region[" + region.id.to_s + "][country_id]" %>
<% id = "region_" + region.id.to_s %>

<%= select_tag(id, options_for_select([["unassigned" , "0" ]] +
                     Country.to_dropdown, region.country_id),

{:name => name} ) %> This give us:

<select id="region_3" name="region[3][country_id]">
  <option value="0">unassigned</option>
  <option selected="selected" value="12">England</option>

NB: we’re using the handy acts_as_dropdown plugin (http://delynnberry.com/projects/acts-as-dropdown/) but we could just as easily prepare the select list with map / collect as above.

July 23, 2008 - (v1.2.0 - v2.1.0)
10 thanks

:only, :except and passing in multiple parameters

To specify that the filter should be applied to or excluded from given controller actions, use the :only and :except parameters. To pass in multiple controller actions use an array:

before_filter :authorize, :except => [:index, :show]
before_filter :authorize, :only => :delete