Options

annaswims Jul 7, 2009 5 thanks

Available options are (none of these exists by default):

* :limit - Requests a maximum column length. This is number of characters for :string and :text columns and number of bytes for :binary and :integer columns.
* :default - The column‘s default value. Use nil for NULL.
* :null - All...

Pretty Printing Routes

nathaniel Jul 7, 2009

if you'd like to check out your routes in the console, you can do something like: routes = ActionController::Routing::Routes # which will return a RouteSet puts routes.routes

which'll give you a nice output like: GET /messages/ {:action=>"index", :controller=...

Example

marcelo_murad Jun 30, 2009 4 thanks

User = Struct.new(:name, :phone)

marc = User.new("Marc", "555-5555")

:selected

nachocab Jun 29, 2009 2 thanks

If you want some object to be selected by default, be sure to use its id, not the whole object.

collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:selected => current_book.authors.map(&:id)}) #=> :selected => [1,2,3,4]

and not collection_select(:post, :author_id,...

Returns a copy of the attribute contents

tadman Jun 25, 2009 1 thank

As szeryf notes, this is a really expensive method, but another important remark is that the contents returned are a copy of the actual values.

model.attributes['name'] # => 'Joe'
model.attributes['name'] = 'Jim'
model.attributes['name'] # => 'Joe' still
model.name # => 'Joe'

This...

Antonym of empty?

szeryf Jun 25, 2009 2 thanks

The antonym of empty? is Enumerable#any? method:

[].empty?  #=> true
[].any?    #=> false

[1].empty? #=> false
[1].any?   #=> true

Be cautious however, if your array might contain nil's or false's:

[false, nil].any? #=> false

Optional local assigns

Manfred Jun 22, 2009 3 thanks

When you have a partial with optional local assigns, for instance:

<%= render :partial => 'articles/preview' %>
<%= render :partial => 'articles/preview', :locals => { :show_call_out => true } %>

And you don't want the partial to break when the local isn't assigned, you can reference it th...

Important note

alloy Jun 18, 2009 12 thanks

It has been said that “it can be compared to, but isn’t the same thing as”:

class Bar
class << self
  attr_accessor :greeting
end
end

Which is true. However, they are “inherited” isn't exactly the case. Rather, cattr_accessor uses class variables.

The probl...

Skipping validation

andrewmcdonough Jun 17, 2009 3 thanks

Unlike the save method, you can't pass false to update_attributes to tell it to skip validation. Should you wish to do this (consider carefully if this is wise) update the attributes explicitly then call save and pass false:

@model_name.attributes = params[:model_name]
@model_name.save fa...

cattr_accessor_with_default

Oleg Jun 12, 2009 3 thanks

Class attribute assessors are neat if you want to set up modifiable constant-like varibles. This is how you'd normally set it up:

module MyPlugin
class Conf
  @@awesome_level = 'huge'
  cattr_accessor :awesome_level
end
end

Then you can call and modify it like this:

>>...