Screencast: Fortgeschrittene Abfragen in Rails 3

In dieser Woche behandelt Ryan fortgeschrittene Abfragemöglichkeiten in Rails 3. Er zeigt wie “named scopes” eingesetzt werden können und es gibt dabei auch eine Einführung in Arel. Arel wird mit Rails 3 eingeführt und wird die Erstellung von DB-Queries vereinfachen und verbessern.

 

Download:

Download(33.9 MB, 9:26)
Alternativer Downloadfür iPod & Apple TV(47 MB, 9:26)

 

Resourcen:

 

Quellcode:

# 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

Eingestellt am 24.05.2010 um 21:15

Kategorie: Tutorials

Be Sociable, Share!

Kommentare für diesen Artikel wurden geschlossen.

Suchen auf rubyonrails.de

Aktuelle Rails Version + Abhängigkeiten:

Gem rails-3.0.0
actionmailer (= 3.0.0, runtime)
actionpack (= 3.0.0, runtime)
activerecord (= 3.0.0, runtime)
activeresource (= 3.0.0, runtime)
activesupport (= 3.0.0, runtime)
bundler (~> 1.0.0, runtime)
railties (= 3.0.0, runtime)

Rails auf Rubyforge
Rails auf Github