· 1 min read

Screencast: ActiveRecord Anfragen in Rails 3

Download:

Download (17.8 MB, 10:43) Alternativer Download für iPod & Apple TV (13.7 MB, 10:43)

Resourcen:

Quellcode:

[ruby] # Article.find( :all, :order => “published_at desc”, :limit => 10) Article.order(“published_at desc”).limit(10)

# Article.find(:all, :conditions => [“published_at <= ?”, Time.now], :include => :comments) Article.where(“published_at <= ?”, Time.now).includes(:comments)

# Article.find(:first, :order => “published_at desc”) Article.order(“published_at”).last

# rails console Article.all articles = Article.order(“name”) articles.all articles.first Article.recent.all puts Article.recent.to_sql

# articles_controller.rb @articles = Article.order(“name”)

if params[:hidden] @articles = @articles.where(:hidden => (params[:hidden] == “1”)) end

# models/active_record.rb scope :visible, where(“hidden != ?”, true) scope :published, lambda { where(“published_at <= ?”, Time.zone.now) } scope :recent, visible.published.order(“published_at desc”) [/ruby]

Back to Blog