Flowdock

Notes posted to Ruby on Rails

RSS feed
October 2, 2008
1 thank

Setting ttl

Use the expires_in option to set a TTL for the cached item.

Rails.cache.write("top_items", :expires_in => 5.minutes)

You can use this with the fetch method as well:

Rails.cache.fetch("top_items", :expires_in => 5.minutes) do
  # some very expensive calculations
end

Note: this only works with supporting cache stores, like the MemCacheStore

October 1, 2008
5 thanks
September 30, 2008
5 thanks

If you're not using resource

If you don’t use resource for your remote_form_for, then :url option is necessary.

For example:

<% remote_form_for "not_resource" do |f| ... %> 

won’t work. But with :url option, it will:

<% remote_form_for "not_resource", 
     :url => { :controller => "recommend", :action => "send" } do ... %> 
September 28, 2008 - (<= v2.1.0)
3 thanks

has_one Nesting in Rails 2.0

Routers:

map.resources :user, :has_one => [:avatar]

Views:

form_for [@user, @avatar], :url => user_avatar_url(@user) do |f|
...
end
September 26, 2008 - (v1.2.6 - v2.1.0)
5 thanks

Pass the observed fields value as a parameter in the Ajax request

Use encodeURIComponent with with to pass an encoded value as a parameter (POST or GET) of the AJAX request. For example:

<%= observe_field :company_id,

:url => {:action => ‘facilities’, :only_path => false}, :with => “‘company=’ + encodeURIComponent(value)” %> Also, setting only_path => false for the URL ensures that the full URL (including host and protocol) is used for the AJAX request.

September 26, 2008
4 thanks

Example of composed_of composition class implementation

If we have following code in model:

composed_of :temperature, :mapping => %w(celsius)

Then our composition class can be this:

class Temperature
  def initialize(celsius)
    @celsius = celsius
  end

  # This method is called by ActiveRecord, when record is saved.
  # Result of this method will be stored in table in "celsius" field,
  # and later when the record is loaded again, this will go to 
  # our Temperature#new constructor.
  def celsius
    @celsius
  end

  # This is example of method that we can add to make this composition useful.
  def farenheit 
    @celsius * 9/5 + 32
  end
end
September 25, 2008
4 thanks

has_many :through

It’s is recommended to use has_many :through association instead of has_and_belongs_to_many. has_many :through is better supported and generally easier to work with once you grasp the idea.

September 25, 2008 - (v2.1.0)
3 thanks

Compare old and new form for

Old form for

<% form_for :user, :url => users_path do %>
  <%= render :partial => 'form' %>
  <%= submit_tag 'Create' %>
<% end %>

New form for

<% form_for(@user) do |f| %>
  <%= render :partial => f %>
  <%= submit_tag 'Create' %>
<% end %>
September 25, 2008
22 thanks

All methods

create_table :table do |t|

  t.column # adds an ordinary column. Ex: t.column(:name, :string)
  t.index # adds a new index.
  t.timestamps
  t.change # changes the column definition. Ex: t.change(:name, :string, :limit => 80)
  t.change_default # changes the column default value.
  t.rename # changes the name of the column.
  t.references
  t.belongs_to
  t.string
  t.text
  t.integer
  t.float
  t.decimal
  t.datetime
  t.timestamp
  t.time
  t.date
  t.binary
  t.boolean
  t.remove
  t.remove_references
  t.remove_belongs_to
  t.remove_index
  t.remove_timestamps
end
September 25, 2008
18 thanks

All methods

change_table :table do |t|

  t.column # adds an ordinary column. Ex: t.column(:name, :string)
  t.index # adds a new index.
  t.timestamps
  t.change # changes the column definition. Ex: t.change(:name, :string, :limit => 80)
  t.change_default # changes the column default value.
  t.rename # changes the name of the column.
  t.references
  t.belongs_to
  t.string
  t.text
  t.integer
  t.float
  t.decimal
  t.datetime
  t.timestamp
  t.time
  t.date
  t.binary
  t.boolean
  t.remove
  t.remove_references
  t.remove_belongs_to
  t.remove_index
  t.remove_timestamps
end
September 25, 2008
2 thanks

Support for the option through

class Magazine < ActiveRecord::Base

  has_many :subscriptions
end

class Subscription < ActiveRecord::Base
  belongs_to :magazine
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :subscriptions
  has_one :magazine, :through => :subscriptions, :conditions => ['subscriptions.active = ?', true]
end
September 25, 2008
6 thanks

Expressions in the sum method

Person.sum(“2 * age”)

Person.sum(:age, :conditions=>'1 = 2')
September 23, 2008 - (<= v2.1.0)
3 thanks

Method description from Rails 2.0

If text is longer than length, text will be truncated to the length of length (defaults to 30) and the last characters will be replaced with the truncate_string (defaults to “…”).

Examples

truncate("Once upon a time in a world far far away", 14)
# => Once upon a...

truncate("Once upon a time in a world far far away")
# => Once upon a time in a world f...

truncate("And they found that many people were sleeping better.", 25, "(clipped)")
# => And they found that many (clipped)

truncate("And they found that many people were sleeping better.", 15, "... (continued)")
# => And they found... (continued)
September 23, 2008 - (<= v2.1.0)
1 thank

Description copied from Rails 2.0

Extracts an excerpt from text that matches the first instance of phrase. The radius expands the excerpt on each side of the first occurrence of phrase by the number of characters defined in radius (which defaults to 100). If the excerpt radius overflows the beginning or end of the text, then the excerpt_string will be prepended/appended accordingly. If the phrase isn‘t found, nil is returned.

Examples

excerpt('This is an example', 'an', 5)
# => "...s is an examp..."

excerpt('This is an example', 'is', 5)
# => "This is an..."

excerpt('This is an example', 'is')
# => "This is an example"

excerpt('This next thing is an example', 'ex', 2)
# => "...next t..."

excerpt('This is also an example', 'an', 8, '<chop> ')
# => "<chop> is also an example"
September 23, 2008
1 thank

Assert empty option in select tag

assert_tag :tag => “select”, :attributes => {:id => “to_airport_id”},

:child => {:tag => "option", :attributes => {:value => ""}, :content => "--select--"},
:children => {:count => 4}
September 22, 2008
0 thanks

Different Options

The docs don’t give any detail to what options are available, so I dug around and I think the only to options are :prompt and :include_blank

September 21, 2008 - (>= v2.1.0)
0 thanks

Test Example

UserMailerTest Example

class UserMailerTest < ActionMailer::TestCase
  tests UserMailer

  def test_welcome_mail
    user = users(:quentin)

    MyMailer.deliver_welcome_email
    assert !ActionMailer::Base.deliveries.empty?

    sent = ActionMailer::Base.deliveries.first
    assert_equal [@user.email], sent.to
    assert_equalexpected subject”, sent.subject
    assert sent.body =~ /^Welcome to my App/
    assert sent.body =~ /^Username: #{@user.login}$/
    assert sent.body =~ /^Password: [a-z0-9]{10}$/i
  end
end

This example is a modified version of the one in this blog post:

http://sablog.com/archives/2006/03/14/how-to-test-actionmailer-in-ruby-on-rails

September 21, 2008
1 thank

resourceful

auto_discovery_link_tag :atom, movies_url(:format=>‘atom’), :title=>‘New movies’

to produce the feed:

respond_to do |wants|
  wants.html
  wants.atom {render :action=>'index',:layout=>false}
end
September 19, 2008 - (v2.1.0)
3 thanks

:expires_in option

If you need :expires_in functionality in Rails 2.1, you can use this plugin:

http://github.com/nickpad/rails-caches-action-patch/tree/master

September 18, 2008
1 thank

unobstrusive label tag

just use

label_tag('a_a','a_a') 

and it works, just not ment for pure decorative labels :)

September 17, 2008
4 thanks

Turn layout off with render

Thats awkward, but the code below does not turn layout off:

render :action => "short_goal", :layout => nil

you must use false

render :action => "short_goal", :layout => false
September 17, 2008
0 thanks

removes underscores -> do not use for images etc

example

#does not work
label_tag('aa'+image_tag('x_x.gif')) 
September 16, 2008
8 thanks

print standard-looking messages during migration

Within a migration file you can use the say_with_time method to print out informational messages that match the style of standard migration messages. See the say method also.

say_with_time "migrate existing data" do
  # ... execute migration sql ...
end
#=> "-- migrate existing data"
#=> "   -> 0.0299s"
September 16, 2008
1 thank

print standard-looking messages during migration

Within a migration file you can use the say method to print out informational messages that match the style of standard migration messages. The say_with_time method is also pretty great.

say "migrate existing data"
  #=> "-- migrate existing data"
# ... execute migration sql ...
say "updated all records", :subitem
  #=> "   -> updated 5 records"
September 15, 2008
4 thanks

:null => false

To not allow a column to have a NULL value, pass :null => false. Seems silly, but that’s it.

September 15, 2008 - (<= v2.1.0)
8 thanks
September 13, 2008
0 thanks

Examples

Some usages:

Code example

Time.new.months_ago(1) # => Wed Aug 13 10:56:32 -0300 2008

Date.today.month_ago(7) # => Qua, 13 Fev 2008

Time.new.months_ago(1).to_s(:db) # => “2008-08-13 10:57:56”

September 11, 2008 - (<= v2.1.0)
4 thanks

Information on 'ModelName.transaction'

If you are looking for information about:

ModelName.transaction do
  ...
end

or

transaction do
  ...
end

see ActiveRecord::Transactions::ClassMethods

September 11, 2008 - (<= v2.1.0)
4 thanks

Information on 'ModelName.transaction'

If you are looking for information about:

ModelName.transaction do
  ...
end

or

transaction do
  ...
end

see ActiveRecord::Transactions::ClassMethods