48.1. Active Record Queries in Rails 3

Odkazy:

class Product < ActiveRecord::Base
  belongs_to :category
  scope :discontinued, where(:discontinued => true)
  # scope :cheaper_than, lambda {|price| where("price < ?", price)}

  def self.cheaper_than(price)
    where("products.price < ?", price)
  end

  scope :cheap, cheaper_than(5)
end
class Category < ActiveRecord::Base
  has_many :products
  scope :with_cheap_products, joins(:products) & Product.cheap
end
Licence Creative Commons
Tento dokument Ruby, jehož autorem je Radek Hnilica, podléhá licenci Creative Commons Uveďte autora-Nevyužívejte dílo komerčně-Zachovejte licenci 3.0 Česká republika .