first
und
all
sind ja schon ganz nett, aber meistens will man
ja doch nach etwas ganz Bestimmten mit einem Suchbegriff suchen
(Query).MacBook:~ xyz$ rails new jukebox [...] MacBook:~ xyz$ cd jukebox MacBook:jukebox xyz$ rails generate model album name release_year:integer [...] MacBook:jukebox xyz$ rake db:migrate [...] MacBook:jukebox xyz$
db/seeds.rb
mit folgendem Inhalt:Album.create("name"=>"Sgt. Pepper's Lonely Hearts Club Band", "release_year"=>1967) Album.create("name"=>"Pet Sounds", "release_year"=>1966) Album.create("name"=>"Revolver", "release_year"=>1966) Album.create("name"=>"Highway 61 Revisited", "release_year"=>1965) Album.create("name"=>"Rubber Soul", "release_year"=>1965) Album.create("name"=>"What's Going On", "release_year"=>1971) Album.create("name"=>"Exile on Main St.", "release_year"=>1972) Album.create("name"=>"London Calling", "release_year"=>1979) Album.create("name"=>"Blonde on Blonde", "release_year"=>1966) Album.create("name"=>"The Beatles", "release_year"=>1968)
MacBook:jukebox xyz$ rake db:setup
db/development.sqlite3 already exists
-- create_table("albums", {:force=>true})
-> 0.0308s
-- initialize_schema_migrations_table()
-> 0.0003s
-- assume_migrated_upto_version(20120426133607, ["/Users/xyz/jukebox/db/migrate"])
-> 0.0005s
MacBook:jukebox xyz$
find
und where
.
Logischerweise stellt sich dem Rails-Anfänger direkt die Frage "Welche
Methode soll ich eher benutzen?". Eine Schwarz/Weiß-Antwort auf diese
Frage fällt mir schwer. Ich selber benutze zu 80 %
where
und bei den restlichen 20 % könnte ich auch
where
benutzen, aber
find
ist dann einen Tick praktischer und der Code
ist besser lesbar. Probieren Sie am besten beide Methoden einmal aus und
entscheiden Sie dann aus dem Bauch heraus.where
zu verstehen.MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.find(2) Album Load (4.0ms) SELECT "albums".* FROM "albums" WHERE "albums"."id" = ? LIMIT 1 [["id", 2]] => #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04"> 1.9.3p194 :002 > Album.find([1,3,7]) Album Load (0.4ms) SELECT "albums".* FROM "albums" WHERE "albums"."id" IN (1, 3, 7) => [#<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 3, name: "Revolver", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 7, name: "Exile on Main St.", release_year: 1972, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">] 1.9.3p194 :003 > exit MacBook:jukebox xyz$
MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.find(5).class Album Load (4.0ms) SELECT "albums".* FROM "albums" WHERE "albums"."id" = ? LIMIT 1 [["id", 5]] => Album(id: integer, name: string, release_year: integer, created_at: datetime, updated_at: datetime) 1.9.3p194 :002 > Album.find([5]).class Album Load (0.2ms) SELECT "albums".* FROM "albums" WHERE "albums"."id" = ? LIMIT 1 [["id", 5]] => Array 1.9.3p194 :003 > Album.find([1,3,7]).class Album Load (0.4ms) SELECT "albums".* FROM "albums" WHERE "albums"."id" IN (1, 3, 7) => Array 1.9.3p194 :004 > exit MacBook:jukebox xyz$
where
(siehe „where“) benutzen,
find_by_*
(siehe „Attribut-basierendes
find_by_*, find_last_by_* und find_all_by_* (find_by_attributes)“) oder mit der Methode exists?
(siehe „exists?“) vorher prüfen, ob der Datensatz
existiert:[24]MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.last Album Load (0.1ms) SELECT "albums".* FROM "albums" ORDER BY "albums"."id" DESC LIMIT 1 => #<Album id: 10, name: "The Beatles", release_year: 1968, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04"> 1.9.3p194 :002 > Album.find(50) Album Load (33.6ms) SELECT "albums".* FROM "albums" WHERE "albums"."id" = ? LIMIT 1 [["id", 50]] ActiveRecord::RecordNotFound: Couldn't find Album with id=50 from /Users/xyz/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/relation/finder_methods.rb:340:in `find_one' from /Users/xyz/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/relation/finder_methods.rb:311:in `find_with_ids' from /Users/xyz/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/relation/finder_methods.rb:107:in `find' from /Users/xyz/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/querying.rb:5:in `find' from (irb):2 from /Users/xyz/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start' from /Users/xyz/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start' from /Users/xyz/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>' 1.9.3p194 :003 > Album.exists?(50) Album Exists (0.2ms) SELECT 1 FROM "albums" WHERE "albums"."id" = 50 LIMIT 1 => false 1.9.3p194 :004 > exit MacBook:jukebox xyz$
find_by_*
ist eine
„magische“ Methode, denn sie kann von Ihnen dynamisch
erweitert werden (deshalb werden diese Methoden auch "dynamic finders"
genannt). Die Funktionalität ist die gleiche wie die der Methode
where
(siehe „where“), aber die Lesbarkeit des Codes wird
besser (jedenfalls bei wenigen Parametern). Man kann treffend über den
Sinn oder den Unsinn streiten. Probieren Sie es einmal aus, und
entscheiden Sie selber, welchen Weg Sie gehen wollen.where
und dann
find_by_*
und
find_last_by_*
für gleiche Abfragen:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where( :release_year => 1966).first Album Load (0.2ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 1966 LIMIT 1 => #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04"> 1.9.3p194 :002 > Album.find_by_release_year(1966) Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 1966 LIMIT 1 => #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04"> 1.9.3p194 :003 > Album.where( :release_year => 1966 ).last Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 1966 ORDER BY "albums"."id" DESC LIMIT 1 => #<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04"> 1.9.3p194 :004 > Album.find_last_by_release_year(1966) Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 1966 ORDER BY "albums"."id" DESC LIMIT 1 => #<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04"> 1.9.3p194 :005 > exit MacBook:jukebox xyz$
where
gibt
find_by_*
entweder
ein Objekt der gesuchten Klasse (hier Album
)
oder nil
zurück. where
gibt immer ein Array zurück! Wenn man auf diesen
Unterschied beim Programmieren nicht achtet, bekommt man über kurz
oder lang einen Fehler an dieser Stelle.MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.find_by_release_year(1972) Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 1972 LIMIT 1 => #<Album id: 7, name: "Exile on Main St.", release_year: 1972, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04"> 1.9.3p194 :002 > Album.find_by_release_year(1972).class Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 1972 LIMIT 1 => Album(id: integer, name: string, release_year: integer, created_at: datetime, updated_at: datetime) 1.9.3p194 :003 > Album.find_by_release_year(2010) Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 2010 LIMIT 1 => nil 1.9.3p194 :004 > Album.find_by_release_year(2010).class Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 2010 LIMIT 1 => NilClass 1.9.3p194 :005 > exit MacBook:jukebox xyz$
find_all_by_*
gibt
wie where
immer ein Array zurück:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.find_all_by_release_year(2020).class Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 2020 => Array 1.9.3p194 :002 > Album.find_all_by_release_year(1966).class Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 1966 => Array 1.9.3p194 :003 > exit MacBook:jukebox xyz$
find_by_*
,
find_last_by_*
und
find_all_by_*
lassen sich auch mehrere Such-Attribute mit and
verketten. Unsere aktuellen Datensätze lassen keine richtig guten
Beispiele dafür zu. Deshalb ein paar wenig sinnvolle, aber die Methode
beschreibende Abfragen, in der mit and
Attribute
verkettet werden:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.find_by_id_and_release_year(1, 1967) Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."id" = 1 AND "albums"."release_year" = 1967 LIMIT 1 => #<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04"> 1.9.3p194 :002 > Album.find_all_by_id_and_release_year(1, 1967) Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."id" = 1 AND "albums"."release_year" = 1967 => [#<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">] 1.9.3p194 :003 > Album.find_by_id_and_name(5, 'The Beatles') Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."id" = 5 AND "albums"."name" = 'The Beatles' LIMIT 1 => nil 1.9.3p194 :004 > Album.find_all_by_id_and_name(5, 'The Beatles') Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."id" = 5 AND "albums"."name" = 'The Beatles' => [] 1.9.3p194 :005 > exit MacBook:jukebox xyz$
find_or_create_by_*
in einem Schritt erledigen (achten Sie auf den SQL-Code):MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > beatles = Album.find_or_create_by_name('The Beatles') Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."name" = 'The Beatles' LIMIT 1 => #<Album id: 10, name: "The Beatles", release_year: 1968, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04"> 1.9.3p194 :002 > ray_charles = Album.find_or_create_by_name('Crying Time') Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."name" = 'Crying Time' LIMIT 1 (0.1ms) begin transaction SQL (8.5ms) INSERT INTO "albums" ("created_at", "name", "release_year", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 30 Apr 2012 14:17:14 UTC +00:00], ["name", "Crying Time"], ["release_year", nil], ["updated_at", Mon, 30 Apr 2012 14:17:14 UTC +00:00]] (1.4ms) commit transaction => #<Album id: 11, name: "Crying Time", release_year: nil, created_at: "2012-04-30 14:17:14", updated_at: "2012-04-30 14:17:14"> 1.9.3p194 :003 > exit MacBook:jukebox xyz$
find_or_create_by_*
sollte man immer sicher sein, dass der eventuell neu anzulegende
Datensatz in sich selber valide ist. Dies lässt sich einfach mit der
Methode valid?
realisieren (siehe
Abschnitt 4.15, „Validierung
(Validation)“). Ein nicht valider
Datensatz wird von find_or_create_by_*
nicht abgespeichert!create
auch noch das
release_year
angeben wollen, so geht das auch mit
find_or_create_by_*
:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > crying_time = Album.find_or_create_by_name('Genius Loves Company', :release_year => 2004) Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."name" = 'Genius Loves Company' LIMIT 1 (0.1ms) begin transaction SQL (5.2ms) INSERT INTO "albums" ("created_at", "name", "release_year", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 30 Apr 2012 14:21:03 UTC +00:00], ["name", "Genius Loves Company"], ["release_year", 2004], ["updated_at", Mon, 30 Apr 2012 14:21:03 UTC +00:00]] (3.5ms) commit transaction => #<Album id: 12, name: "Genius Loves Company", release_year: 2004, created_at: "2012-04-30 14:21:03", updated_at: "2012-04-30 14:21:03"> 1.9.3p194 :002 > exit MacBook:jukebox xyz$
find_or_initialize_by_*
arbeitet wie
find_or_create_by_*
.
Allerdings gibt es einen entscheidenden Unterschied:
find_or_initialize_by_*
speichert einen neuen Datensatz nicht ab. Das muss später per
save
erfolgen. Beispiel:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > i_got_a_woman = Album.find_or_initialize_by_name('I Got a Woman', :release_year => 1955) Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."name" = 'I Got a Woman' LIMIT 1 => #<Album id: nil, name: "I Got a Woman", release_year: 1955, created_at: nil, updated_at: nil> 1.9.3p194 :002 > Album.last Album Load (0.3ms) SELECT "albums".* FROM "albums" ORDER BY "albums"."id" DESC LIMIT 1 => #<Album id: 12, name: "Genius Loves Company", release_year: 2004, created_at: "2012-04-30 14:21:03", updated_at: "2012-04-30 14:21:03"> 1.9.3p194 :003 > i_got_a_woman.save (0.1ms) begin transaction SQL (6.8ms) INSERT INTO "albums" ("created_at", "name", "release_year", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 30 Apr 2012 14:23:25 UTC +00:00], ["name", "I Got a Woman"], ["release_year", 1955], ["updated_at", Mon, 30 Apr 2012 14:23:25 UTC +00:00]] (3.5ms) commit transaction => true 1.9.3p194 :004 > Album.last Album Load (0.3ms) SELECT "albums".* FROM "albums" ORDER BY "albums"."id" DESC LIMIT 1 => #<Album id: 13, name: "I Got a Woman", release_year: 1955, created_at: "2012-04-30 14:23:25", updated_at: "2012-04-30 14:23:25"> 1.9.3p194 :005 > exit MacBook:jukebox xyz$
where
und
find_all_by_*
sich
oft gleich anfühlen, so gibt es bei einigen Szenarien deutliche
Performance-Unterschiede. Vereinfacht gesagt: Mit
where
machen Sie nie einen Fehler, da
where
„Lazy Loading“ benutzt. Auf
Lazy Loading gehen wir in
„Lazy Loading“ dediziert ein. Hier aber schon mal ein
kleiner Vorgeschmack:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where(:release_year => 1966).count (0.2ms) SELECT COUNT(*) FROM "albums" WHERE "albums"."release_year" = 1966 => 3 1.9.3p194 :002 > Album.find_all_by_release_year(1966).count Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 1966 => 3 1.9.3p194 :003 > exit MacBook:jukebox xyz$
where
und
count
erstellt eine SQL-Abfrage, die das
Berechnen des Ergebnisses der SQL-Datenbank überlässt. Die Kombination
der Methoden
find_all_by_*
und
count
fragt erst alle Datensätze bei der
Datenbank ab, speichert diese dann in einem Array und zählt
anschließend die Einträge in diesem Array. Das macht bei unserer
Mini-Datenbank kaum einen Unterschied (0.1 ms). Wenn Sie aber mit
einer sehr großen Datenbank arbeiten, dann wollen Sie die maximale
Performance der SQL-Datenbank ausnutzen und nicht erst Daten hin- und
herschieben.where
kann man nach
bestimmten Werten in der Datenbank suchen. Suchen wir mal alle Alben aus
dem Jahr 1966:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where( :release_year => 1966 ) Album Load (0.2ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 1966 => [#<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 3, name: "Revolver", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">] 1.9.3p194 :002 > Album.where( :release_year => 1966 ).count (0.3ms) SELECT COUNT(*) FROM "albums" WHERE "albums"."release_year" = 1966 => 3 1.9.3p194 :003 > exit MacBook:jukebox xyz$
where
auch nach
Ranges (Bereichen, siehe
„Range“) suchen:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where( :release_year => 1960..1966 ) Album Load (0.2ms) SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1966) => [#<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 3, name: "Revolver", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 4, name: "Highway 61 Revisited", release_year: 1965, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 5, name: "Rubber Soul", release_year: 1965, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">] 1.9.3p194 :002 > Album.where( :release_year => 1960..1966 ).count (0.3ms) SELECT COUNT(*) FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1966) => 5 1.9.3p194 :003 > exit MacBook:jukebox xyz$
MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where( :release_year => 1960..1966, :id => 1..5 ) Album Load (0.2ms) SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1966) AND ("albums"."id" BETWEEN 1 AND 5) => [#<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 3, name: "Revolver", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 4, name: "Highway 61 Revisited", release_year: 1965, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 5, name: "Rubber Soul", release_year: 1965, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">] 1.9.3p194 :002 > Album.where( :release_year => 1960..1966, :id => 1..5 ).count (0.3ms) SELECT COUNT(*) FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1966) AND ("albums"."id" BETWEEN 1 AND 5) => 4 1.9.3p194 :003 > exit MacBook:jukebox xyz$
MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where( :release_year => [1966, 1968] ) Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" IN (1966, 1968) => [#<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 3, name: "Revolver", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 10, name: "The Beatles", release_year: 1968, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">] 1.9.3p194 :002 > Album.where( :release_year => [1966, 1968] ).count (0.3ms) SELECT COUNT(*) FROM "albums" WHERE "albums"."release_year" IN (1966, 1968) => 4 1.9.3p194 :003 > exit MacBook:jukebox xyz$
where
ist immer ein
Array. Auch wenn nur ein Treffer drin ist oder auch wenn gar kein
Treffer zurückgegeben wird. Wenn Sie den ersten Treffer suchen, dann
müssen Sie where
mit der Methode
first
verketten:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where( :release_year => [1966, 1968] ).first Album Load (0.2ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" IN (1966, 1968) LIMIT 1 => #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13"> 1.9.3p194 :002 > Album.where( :release_year => [1966, 1968] ).first.class Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" IN (1966, 1968) LIMIT 1 => Album(id: integer, name: string, release_year: integer, created_at: datetime, updated_at: datetime) 1.9.3p194 :003 > exit MacBook:jukebox xyz$
http://de.wikipedia.org/wiki/SQL-Injection
).MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where( 'name like ?', '%on%' ) Album Load (0.2ms) SELECT "albums".* FROM "albums" WHERE (name like '%on%') => [#<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 6, name: "What's Going On", release_year: 1971, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 7, name: "Exile on Main St.", release_year: 1972, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 8, name: "London Calling", release_year: 1979, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">] 1.9.3p194 :002 > Album.where( 'name like ?', '%on%' ).count (0.2ms) SELECT COUNT(*) FROM "albums" WHERE (name like '%on%') => 5 1.9.3p194 :003 > exit MacBook:jukebox xyz$
MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where( 'release_year > ?', 1964 ).count (0.1ms) SELECT COUNT(*) FROM "albums" WHERE (release_year > 1964) => 10 1.9.3p194 :002 > exit MacBook:jukebox xyz$
MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where( 'name like ? AND release_year > ?', '%on%', 1970 ).count (0.2ms) SELECT COUNT(*) FROM "albums" WHERE (name like '%on%' AND release_year > 1970) => 3 1.9.3p194 :002 > exit MacBook:jukebox xyz$
apfelmus
der gesuchte
String enthalten ist, dann kann folgendermaßen danach gesucht
werden:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > apfelmus = 'ing' => "ing" 1.9.3p194 :002 > Album.where( 'name like ?', "%#{apfelmus}%").count (0.1ms) SELECT COUNT(*) FROM "albums" WHERE (name like '%ing%') => 2 1.9.3p194 :003 > exit MacBook:jukebox xyz$
MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where( "name like '%on%'" ).count (0.5ms) SELECT COUNT(*) FROM "albums" WHERE (name like '%on%') => 5 1.9.3p194 :002 > exit
http://de.wikipedia.org/wiki/SQL-Injection
).where
und damit Lazy Loading verwendet. Dies ist ein
Mechanismus, der eine Datenbank-Abfrage erst dann stellt, wenn der
weitere Programmablauf nicht ohne das Ergebnis dieser Abfrage realisiert
werden kann. Bis dahin wird die Anfrage als
ActiveRecord::Relation
gespeichert. (Das
Gegenteil von Lazy Loading
nennt man übrigens Eager
Loading.)MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where(:release_year => 1966) Album Load (0.2ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 1966 => [#<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 3, name: "Revolver", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">] 1.9.3p194 :002 > Album.where(:release_year => 1966).class => ActiveRecord::Relation 1.9.3p194 :003 > Album.where(:release_year => 1966).to_sql => "SELECT \"albums\".* FROM \"albums\" WHERE \"albums\".\"release_year\" = 1966" 1.9.3p194 :004 > exit MacBook:jukebox xyz$
Album.where(:release_year => 1966)
eine
ActiveRecord::Relation
ist, warum bekommen wir
dann in der Console ein
Array ausgegeben? Rails will uns das Entwicklerleben etwas einfacher
machen und zeigt uns quasi automatisch das Ergebnis der Methode
all
an:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where(:release_year => 1966).class => ActiveRecord::Relation 1.9.3p194 :002 > Album.where(:release_year => 1966).all.class Album Load (0.2ms) SELECT "albums".* FROM "albums" WHERE "albums"."release_year" = 1966 => Array 1.9.3p194 :003 > exit MacBook:jukebox xyz$
a
immer
weiter definiert und erst ganz zum Schluss (beim Aufruf der Methode
all
) würde bei einem Produktivsystem die
Datenbank-Abfrage wirklich getätigt. Mit der Methode to_sql
kann man sich immer das
aktuelle SQL-Query ausgeben lassen.MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > a = Album.where(:release_year => 1965..1968) Album Load (0.2ms) SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1965 AND 1968) => [#<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 3, name: "Revolver", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 4, name: "Highway 61 Revisited", release_year: 1965, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 5, name: "Rubber Soul", release_year: 1965, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 10, name: "The Beatles", release_year: 1968, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">] 1.9.3p194 :002 > a.class => ActiveRecord::Relation 1.9.3p194 :003 > a = a.order(:release_year) Album Load (0.5ms) SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1965 AND 1968) ORDER BY release_year => [#<Album id: 4, name: "Highway 61 Revisited", release_year: 1965, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 5, name: "Rubber Soul", release_year: 1965, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 3, name: "Revolver", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 10, name: "The Beatles", release_year: 1968, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">] 1.9.3p194 :004 > a = a.limit(3) Album Load (0.5ms) SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1965 AND 1968) ORDER BY release_year LIMIT 3 => [#<Album id: 4, name: "Highway 61 Revisited", release_year: 1965, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 5, name: "Rubber Soul", release_year: 1965, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">, #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04">] 1.9.3p194 :005 > exit MacBook:jukebox xyz$
MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where(:release_year => 1970..1979).sum(:release_year) (3.1ms) SELECT SUM("albums"."release_year") AS sum_id FROM "albums" WHERE ("albums"."release_year" BETWEEN 1970 AND 1979) => 5922 1.9.3p194 :002 > Album.where(:release_year => 1970..1979).order(:name).sum(:release_year) (0.3ms) SELECT SUM("albums"."release_year") AS sum_id FROM "albums" WHERE ("albums"."release_year" BETWEEN 1970 AND 1979) => 5922 1.9.3p194 :003 > exit MacBook:jukebox xyz$
order
bei sum
völlig
irrelevant ist und es deshalb direkt herausgenommen.exists?
. Sie gibt als Ergebnis
true
oder false
zurück:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.first Album Load (0.1ms) SELECT "albums".* FROM "albums" LIMIT 1 => #<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-30 13:45:04", updated_at: "2012-04-30 13:45:04"> 1.9.3p194 :002 > Album.exists?(1) Album Exists (0.2ms) SELECT 1 FROM "albums" WHERE "albums"."id" = 1 LIMIT 1 => true 1.9.3p194 :003 > Album.exists?(10000) Album Exists (0.2ms) SELECT 1 FROM "albums" WHERE "albums"."id" = 10000 LIMIT 1 => false 1.9.3p194 :004 > exit MacBook:jukebox xyz$
exists?
können Sie sich mit ri
ActiveRecord::FinderMethods.exists? anzeigen lassen.order
sortiert werden. Beispiel: Alle Alben aus
den 60ern nach Namen sortiert:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where( :release_year => 1960..1969 ).order(:name) Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) ORDER BY name => [#<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 4, name: "Highway 61 Revisited", release_year: 1965, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 3, name: "Revolver", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 5, name: "Rubber Soul", release_year: 1965, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 10, name: "The Beatles", release_year: 1968, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">] 1.9.3p194 :002 > exit MacBook:jukebox xyz$
order
definierte Reihenfolge umkehren:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where(:release_year => 1960..1969).order(:name).reverse_order Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) ORDER BY name DESC => [#<Album id: 10, name: "The Beatles", release_year: 1968, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">, #<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">, #<Album id: 5, name: "Rubber Soul", release_year: 1965, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">, #<Album id: 3, name: "Revolver", release_year: 1966, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">, #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">, #<Album id: 4, name: "Highway 61 Revisited", release_year: 1965, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">, #<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">] 1.9.3p194 :002 > exit MacBook:jukebox xyz$
limit
auf einen bestimmten Bereich eingegrenzt
werden.MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where( :release_year => 1960..1969).limit(5) Album Load (0.2ms) SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) LIMIT 5 => [#<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 3, name: "Revolver", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 4, name: "Highway 61 Revisited", release_year: 1965, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 5, name: "Rubber Soul", release_year: 1965, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">] 1.9.3p194 :002 > exit MacBook:jukebox xyz$
MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.order(:name).limit(5) Album Load (0.3ms) SELECT "albums".* FROM "albums" ORDER BY name LIMIT 5 => [#<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 7, name: "Exile on Main St.", release_year: 1972, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 4, name: "Highway 61 Revisited", release_year: 1965, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 8, name: "London Calling", release_year: 1979, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">] 1.9.3p194 :002 > exit MacBook:jukebox xyz$
offset
kann die
Startposition der Methode limit
definiert
werden.MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.limit(2) Album Load (0.2ms) SELECT "albums".* FROM "albums" LIMIT 2 => [#<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">] 1.9.3p194 :002 > Album.limit(2).offset(5) Album Load (0.3ms) SELECT "albums".* FROM "albums" LIMIT 2 OFFSET 5 => [#<Album id: 6, name: "What's Going On", release_year: 1971, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 7, name: "Exile on Main St.", release_year: 1972, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">] 1.9.3p194 :003 > exit MacBook:jukebox xyz$
group
kann das Ergebnis
einer Abfrage gruppiert zurückgegeben werden.MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.group(:release_year) Album Load (0.3ms) SELECT "albums".* FROM "albums" GROUP BY release_year => [#<Album id: 5, name: "Rubber Soul", release_year: 1965, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 10, name: "The Beatles", release_year: 1968, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 6, name: "What's Going On", release_year: 1971, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 7, name: "Exile on Main St.", release_year: 1972, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">, #<Album id: 8, name: "London Calling", release_year: 1979, created_at: "2012-04-26 13:40:13", updated_at: "2012-04-26 13:40:13">] 1.9.3p194 :002 > exit MacBook:jukebox xyz$
pluck
gemacht werden.
Wollen Sie alle name-Einträge für alle Alben aus den 1960ern?MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where(:release_year => 1960..1969).pluck(:name) (0.2ms) SELECT name FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) => ["Sgt. Pepper's Lonely Hearts Club Band", "Pet Sounds", "Revolver", "Highway 61 Revisited", "Rubber Soul", "Blonde on Blonde", "The Beatles"] 1.9.3p194 :002 > exit MacBook:jukebox xyz$
pluck
ein
Array.average
lässt sich der
Durchschnitt der Werte in einer bestimmten Spalte in der Tabelle
ausrechnen. Dafür ist unser Datenmaterial natürlich schlecht geeignet.
Als Erstes berechnen wir das Durchschnitts-Veröffentlichungsjahr aller
Alben und danach das Gleiche für die Alben aus den 60ern:MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.average(:release_year) (3.7ms) SELECT AVG("albums"."release_year") AS avg_id FROM "albums" => #<BigDecimal:7fbdd1f01ef0,'0.19685E4',18(45)> 1.9.3p194 :002 > Album.average(:release_year).to_s (0.3ms) SELECT AVG("albums"."release_year") AS avg_id FROM "albums" => "1968.5" 1.9.3p194 :003 > Album.where( :release_year => 1960..1969 ).average(:release_year) (0.3ms) SELECT AVG("albums"."release_year") AS avg_id FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) => #<BigDecimal:7fbdd1b07980,'0.1966142857 142857E4',27(45)> 1.9.3p194 :004 > Album.where( :release_year => 1960..1969 ).average(:release_year).to_s (0.2ms) SELECT AVG("albums"."release_year") AS avg_id FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) => "1966.142857142857" 1.9.3p194 :005 > exit MacBook:jukebox xyz$
count
zählt die Anzahl der
Datensätze.MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.count (0.1ms) SELECT COUNT(*) FROM "albums" => 10 1.9.3p194 :002 > Album.where( :release_year => 1960..1969 ).count (0.2ms) SELECT COUNT(*) FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) => 7 1.9.3p194 :003 > exit MacBook:jukebox xyz$
maximum
kann aus einer
Abfrage der Eintrag mit dem höchsten Wert ausgegeben werden.MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.maximum(:release_year) (0.2ms) SELECT MAX("albums"."release_year") AS max_id FROM "albums" => 1979 1.9.3p194 :002 > exit MacBook:jukebox xyz$
minimum
kann aus einer
Abfrage der Eintrag mit dem niedrigsten Wert ausgegeben werden.MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.minimum(:release_year) (0.1ms) SELECT MIN("albums"."release_year") AS min_id FROM "albums" => 1965 1.9.3p194 :002 > exit MacBook:jukebox xyz$
MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.sum(:release_year) (3.2ms) SELECT SUM("albums"."release_year") AS sum_id FROM "albums" => 19685 1.9.3p194 :002 > exit MacBook:jukebox xyz$
explain
-Methode abgerufen
werden (mit einem puts
kann man sich die Ausgabe schöner
anzeigen lassen):MacBook:jukebox xyz$ rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Album.where(:release_year => 1960..1969) Album Load (0.2ms) SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) => [#<Album id: 1, name: "Sgt. Pepper's Lonely Hearts Club Band", release_year: 1967, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">, #<Album id: 2, name: "Pet Sounds", release_year: 1966, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">, #<Album id: 3, name: "Revolver", release_year: 1966, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">, #<Album id: 4, name: "Highway 61 Revisited", release_year: 1965, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">, #<Album id: 5, name: "Rubber Soul", release_year: 1965, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">, #<Album id: 9, name: "Blonde on Blonde", release_year: 1966, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">, #<Album id: 10, name: "The Beatles", release_year: 1968, created_at: "2012-04-30 14:51:03", updated_at: "2012-04-30 14:51:03">] 1.9.3p194 :002 > Album.where(:release_year => 1960..1969).explain Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) EXPLAIN (0.1ms) EXPLAIN QUERY PLAN SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) => "EXPLAIN for: SELECT \"albums\".* FROM \"albums\" WHERE (\"albums\".\"release_year\" BETWEEN 1960 AND 1969)\n0|0|0|SCAN TABLE albums (~500000 rows)\n" 1.9.3p194 :003 > puts Album.where(:release_year => 1960..1969).explain Album Load (0.3ms) SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) EXPLAIN (0.1ms) EXPLAIN QUERY PLAN SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) EXPLAIN for: SELECT "albums".* FROM "albums" WHERE ("albums"."release_year" BETWEEN 1960 AND 1969) 0|0|0|SCAN TABLE albums (~500000 rows) => nil 1.9.3p194 :004 > exit MacBook:jukebox xyz$
[24] Sie können eine Exception auch mit
rescue
abfangen, aber darauf gehe ich in
diesem Anfängerbuch nicht weiter ein.