- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
WORKING WITH A DATABASE: ACTIVE RECORD in Font
WORKING WITH A DATABASE: ACTIVE RECORD PDF 417 Drawer In None Using Barcode drawer for Font Control to generate, create PDF 417 image in Font applications. www.OnBarcode.comUCC - 12 Maker In None Using Barcode creator for Font Control to generate, create UPC A image in Font applications. www.OnBarcode.comAnd after you ve isolated a single Article object, you can get at its attributes: >> articles[0].title => "RailsConf" What s happening here is that all produces an array, and you access the object at the 0 index and call the title method. You can also use the first method, which all arrays respond to, and get the same result, but with a little more natural syntax: >> articles.first.title => "RailsConf" If you want to iterate over the collection, you can use the each method, which, again, works with all arrays. Here, you loop over the array, extract each item into a variable called article, and print its title attribute using the puts command: >> articles.each {|article| puts article.title} RailsConf Introduction to SQL Introduction to Active Record RubyConf 2010 Rails Pub Nite => [#<Article id: 1,..> #<Article id: 2,..>, #<Article id: 3,..>, #<Article id: 4,..> , #<Article id: 5,..>] Sometimes, you want your results ordered. For example, if you re listing all your articles, you probably want them listed chronologically. To do so, you can use the order method, which accepts as argument the name of the column or columns. For you SQL heroes, it corresponds to the SQL ORDER clause: >> articles = Article.order("published_at") => [#<Article id: 1,..> #<Article id: 2,..>, #<Article id: 3,..>, #<Article id: 4,..> , #<Article id: 5,..>] >> articles.each {|article| puts article.published_at } 2010-02-27 00:00:00 UTC 2010-05-01 04:00:00 UTC 2010-05-01 04:00:00 UTC 2010-05-19 00:00:00 UTC 2010-05-19 00:00:00 UTC => [#<Article id: 1,..> #<Article id: 2,..>, #<Article id: 3,..>, #<Article id: 4,..> , #<Article id: 5,..>] Notice that when you call the order method, it returns an array object as you may have expected. One thing that happens on the background is that Active Record allows you to chain calls to multiple methods before sending the command to the database; so you can call all, followed by order, and some other methods we ll talk about in the next chapter, to create more precise database queries. Also, Active Record is smart enough to use lazy loading, a practice that only hits the database when necessary in this example, when you call the each method. By default, any column is ordered in ascending order (for example, 1 10, or a z). If you want to reverse this to get descending order, use the DESC modifier (the same way you do in SQL, because the value of order parameter is really just a SQL fragment): Quick Response Code Creation In None Using Barcode printer for Font Control to generate, create Quick Response Code image in Font applications. www.OnBarcode.comCode 128 Code Set C Maker In None Using Barcode drawer for Font Control to generate, create Code-128 image in Font applications. www.OnBarcode.comWORKING WITH A DATABASE: ACTIVE RECORD
EAN13 Generation In None Using Barcode drawer for Font Control to generate, create EAN13 image in Font applications. www.OnBarcode.comData Matrix 2d Barcode Encoder In None Using Barcode encoder for Font Control to generate, create ECC200 image in Font applications. www.OnBarcode.com>> articles = Article.order ('published_at DESC') => [#<Article id: 4,..> #<Article id: 5,..>, #<Article id: 2,..>, #<Article id: 3,..> , #<Article id: 1,..>] >> articles.each {|article| puts article.published_at } 2010-05-19 00:00:00 UTC 2010-05-19 00:00:00 UTC 2010-05-01 04:00:00 UTC 2010-05-01 04:00:00 UTC 2010-02-27 00:00:00 UTC => [#<Article id: 4,..> #<Article id: 5,..>, #<Article id: 2,..>, #<Article id: 3,..> , #<Article id: 1,..>] Barcode Creator In None Using Barcode drawer for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.comEncode Code 93 Full ASCII In None Using Barcode creation for Font Control to generate, create Code 9/3 image in Font applications. www.OnBarcode.comFinding with Conditions
Making PDF 417 In Java Using Barcode generator for Android Control to generate, create PDF 417 image in Android applications. www.OnBarcode.comEncoding PDF 417 In Visual Studio .NET Using Barcode creator for VS .NET Control to generate, create PDF 417 image in .NET framework applications. www.OnBarcode.comAlthough finding a record by its primary key is useful, it requires that you know the id to begin with, which isn t always the case. Sometimes you want to find records based on other criteria. This is where conditions come into play. Conditions correspond to the SQL WHERE clause. If you want to find a record by its title, you call the where method and pass a value that contains either a hash of conditions or a SQL fragment. Here, you use a hash of conditions to indicate you want the first article with the title RailsConf: >> Article.where(:title => 'RailsConf').first => #<Article id: 1, title: "RailsConf", body: "RailsConf is the official gathering for Rails devel...", published_at: "2010-02-27 00:00:00", created_at: "2010-05-01 23:12:09", updated_at: "2010-05-01 23:12:09", excerpt: nil, location: nil> Because you use first, you get only one record (the first one in the result set, even if there is more than one result). If you instead use all, you get back a collection, even if the collection has only one item in it: >> Article.where(:title => 'RailsConf').all => [#<Article id: 1, title: "RailsConf", body: "RailsConf is the official gathering for Rails devel...", published_at: "2010-02-27 00:00:00", created_at: "2010-05-01 23:12:09", updated_at: "2010-05-01 23:12:09", excerpt: nil, location: nil>] Notice the square brackets, and remember that they indicate an array. More often than not, when you re doing a all operation, you expect more than one record in return. But all always produces an array, even if that array is empty: >> Article.where(:title => 'Unknown').all => [] Recognizing PDF 417 In Visual Studio .NET Using Barcode recognizer for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comPrint Linear In C#.NET Using Barcode generation for .NET framework Control to generate, create Linear image in Visual Studio .NET applications. www.OnBarcode.comUPC Code Drawer In None Using Barcode encoder for Online Control to generate, create UPC Symbol image in Online applications. www.OnBarcode.comScan Barcode In Visual C#.NET Using Barcode reader for .NET framework Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comData Matrix ECC200 Generator In None Using Barcode encoder for Online Control to generate, create Data Matrix image in Online applications. www.OnBarcode.comDrawing PDF417 In Java Using Barcode creation for Java Control to generate, create PDF417 image in Java applications. www.OnBarcode.comECC200 Printer In Objective-C Using Barcode encoder for iPad Control to generate, create Data Matrix ECC200 image in iPad applications. www.OnBarcode.comCode 128 Code Set C Maker In .NET Framework Using Barcode creation for ASP.NET Control to generate, create Code 128 Code Set B image in ASP.NET applications. www.OnBarcode.comMake QR Code 2d Barcode In None Using Barcode creation for Online Control to generate, create QR Code 2d barcode image in Online applications. www.OnBarcode.comMake PDF417 In Java Using Barcode drawer for Android Control to generate, create PDF 417 image in Android applications. www.OnBarcode.com |
|