Flowdock

Notes posted by tinogomes

RSS feed
March 26, 2012 - (v2.3.2 - v3.1.0)
1 thank

Use exist scopes on default_scope - pay attention

To use exists scopes on default_scope , you can use something like:

class Article < ActiveRecord::Base
  scope :active, proc {
    where("expires_at IS NULL or expires_at > '#{Time.now}'")
  }

  scope :by_newest, order("created_at DESC")

  default_scope by_newest
end

But, if you would add a filter, and it require a lazy evaluate, use block on default_scope declaration, like:

default_scope { active.by_newest }
January 28, 2011 - (>= v1_8_7_72)
1 thank

Passing a block with methods

Code example

Google = Struct.new(:address) do
  def latitude
    -1
  end

  def longitude
    -2
  end

  def with_address
    "with #{address}"
  end
end

g = Google.new("Some Addres")

puts g.address
puts g.latitude
puts g.longitude
puts g.with_address

Result

# >> Some Addres
# >> -1
# >> -2
# >> with Some Addres