· 1 min read
Screencast: Fortgeschrittene Abfragen in Rails 3
Download:
Download(33.9 MB, 9:26) Alternativer Downloadfür iPod & Apple TV(47 MB, 9:26)
Resourcen:
Quellcode:
[ruby] # rails console Product.cheap.to_sql (Category.joins(:products) & Product.cheap).to_sql Category.with_cheap_products.to_sql p = Product.discontinued.build p.discontinued t = Product.arel_table t[:price].eq(2.99) t[:name].matches(“%catan”).to_sql Product.where(t[:price].eq(2.99).or(t[:name].matches(“%catan”)))
# models/product.rb scope :discontinued, where(:discontinued => true)
def self.cheaper_than(price) where(“products.price < ?”, price) end
scope :cheap, cheaper_than(5)
# models/category.rb scope :with_cheap_products, joins(:products) & Product.cheap [/ruby]