From 7bc1ca351523949f6b4ce96018e95e61cbc7719e Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Sat, 1 Sep 2012 17:08:06 -0400 Subject: Convert code blocks into GFM style --- guides/source/active_record_basics.md | 60 +++++++++++++++++------------------ 1 file changed, 30 insertions(+), 30 deletions(-) (limited to 'guides/source/active_record_basics.md') diff --git a/guides/source/active_record_basics.md b/guides/source/active_record_basics.md index 487f8b70f9..a0bb4a8e01 100644 --- a/guides/source/active_record_basics.md +++ b/guides/source/active_record_basics.md @@ -74,28 +74,28 @@ h3. Creating Active Record Models It is very easy to create Active Record models. All you have to do is to subclass the +ActiveRecord::Base+ class and you're good to go: - +```ruby class Product < ActiveRecord::Base end - +``` This will create a +Product+ model, mapped to a +products+ table at the database. By doing this you'll also have the ability to map the columns of each row in that table with the attributes of the instances of your model. Suppose that the +products+ table was created using an SQL sentence like: - +```sql CREATE TABLE products ( id int(11) NOT NULL auto_increment, name varchar(255), PRIMARY KEY (id) ); - +``` Following the table schema above, you would be able to write code like the following: - +```ruby p = Product.new p.name = "Some Book" puts p.name # "Some Book" - +``` h3. Overriding the Naming Conventions @@ -103,29 +103,29 @@ What if you need to follow a different naming convention or need to use your Rai You can use the +ActiveRecord::Base.table_name=+ method to specify the table name that should be used: - +```ruby class Product < ActiveRecord::Base self.table_name = "PRODUCT" end - +``` If you do so, you will have to define manually the class name that is hosting the fixtures (class_name.yml) using the +set_fixture_class+ method in your test definition: - +```ruby class FunnyJoke < ActiveSupport::TestCase set_fixture_class :funny_jokes => 'Joke' fixtures :funny_jokes ... end - +``` It's also possible to override the column that should be used as the table's primary key using the +ActiveRecord::Base.set_primary_key+ method: - +```ruby class Product < ActiveRecord::Base set_primary_key "product_id" end - +``` h3. CRUD: Reading and Writing Data @@ -137,52 +137,52 @@ Active Record objects can be created from a hash, a block or have their attribut For example, given a model +User+ with attributes of +name+ and +occupation+, the +create+ method call will create and save a new record into the database: - +```ruby user = User.create(:name => "David", :occupation => "Code Artist") - +``` Using the +new+ method, an object can be created without being saved: - +```ruby user = User.new user.name = "David" user.occupation = "Code Artist" - +``` A call to +user.save+ will commit the record to the database. Finally, if a block is provided, both +create+ and +new+ will yield the new object to that block for initialization: - +```ruby user = User.new do |u| u.name = "David" u.occupation = "Code Artist" end - +``` h4. Read Active Record provides a rich API for accessing data within a database. Below are a few examples of different data access methods provided by Active Record. - +```ruby # return array with all records users = User.all - +``` - +```ruby # return the first record user = User.first - +``` - +```ruby # return the first user named David david = User.find_by_name('David') - +``` - +```ruby # find all users named David who are Code Artists and sort by created_at in reverse chronological order users = User.where(:name => 'David', :occupation => 'Code Artist').order('created_at DESC') - +``` You can learn more about querying an Active Record model in the "Active Record Query Interface":"active_record_querying.html" guide. @@ -190,20 +190,20 @@ h4. Update Once an Active Record object has been retrieved, its attributes can be modified and it can be saved to the database. - +```ruby user = User.find_by_name('David') user.name = 'Dave' user.save - +``` h4. Delete Likewise, once retrieved an Active Record object can be destroyed which removes it from the database. - +```ruby user = User.find_by_name('David') user.destroy - +``` h3. Validations -- cgit v1.2.3