skip to main |
skip to sidebar
Week 9: Rails Passion Course
- Week 9: ActiveRecord Basics (Topics covered: What is Active Record?, Active Record Object Creation, Find operation, Dynamic Attribute-based Finders, Validation, Migration, Callbacks, Exception Handling)
- Week 9 Resources
- Basics
- ActiveRecord from api.rubyonrails.org
- ActiveRecord::Base API from api.rubyonrails.org
- ActiveRecord tutorial screencast by Gregg (July 2007)
- Advanced ActiveRecord presentation by Obie Fernandez
- Validation
- ActiveRecord::Validations::ClassMethods API document from rubyonrails.com
- Various examples from railsbrains.com
- Migration
- ActiveRecord::Migration API document from railsbrains.com
- Generate a migration from an existing MySQL database posting in ruby-forum.com
- 5522_rails_activerecord.zip (Unzip it in a directory of your choice and read lab document from /jrubyrails_basics/index.html to proceed) (4/19/09: Downloaded)
- Presentation --PDF: 1 slide per page (OR) OpenOffice file (44 pages) (4/21/09: Read)
- Online Lab/Change Log document (This is the same document you will find in the hands-on lab zip file) (105 pages)
- EXERCISE #0: Start MySQL database server. (p.2)
- EXERCISE #1: Creation of ActiveRecord Objects (p.2-39)
- (1.1) Create a new NetBeans project (with JRuby 1.1.4 and WEBrick) (p.3-6)
- (1.2) Create database (p.6-7)
- (1.3) Generate Model (3 attributes) (p.8-10)
- (1.4) Create database table through migration (p.10-19)
- (1.5) Create ActiveRecord objects through Rails Console (p.20-28)
- (1.6) Create database table rows through migration (p.29-39)
- EXERCISE #2: ActiveRecord#find (p.40-44)
- (2.1) find(:all) with various options (p.40-42)
- (2.1.1) With limit, offset, select options (p.40)
- (2.1.2) With order parameter (p.40-41)
- (2.1.3) With conditions parameter (p.41-42)
- (2.2) find(:first) (p.42)
- (2.3) find() with id (p.42)
- (2.4) Dynamic attribute-based finders (p.42-43)
- (2.5) find_by_sql method (p.43)
- (2.6) find_or_create_by_ method (p.43-44)
- (2.7) find() with params (44) -- EMPTY/SKIPPED
- EXERCISE #3: Validation (p.44-75)
- (3.1) Delete all rows of the "users" table through migration (p.44-49)
- (3.2) Perform validation using validation helpers (p.50-60)
- (3.3) Add email field to the users table (p.61-69)
- (3.4) Add validation logic for the "email" field (p.70-71)
- (3.5) Perform validation testing on the email field (p.72-75)
- EXERCISE #4: Migrations (p.76-105)
- (4.1) Add a new column (p.76)
- (4.2) Rename a column - chane "email" to "email_addr" (p.76-83)
- (4.3) Roll back the change - change "email_addr" to "email" (p.84-89)
- (4.4) Display "schema.rb" file that maintains the current migration number (p.90-91)
- (4.5) Exercise with multiple veresions of migration files (p.92-105)
- EXERCISE #5: Statistics (p.106-107)
- EXERCISE #6: Callbacks (p.108--114)
- (6.1) Add callback to the Model (p.108-114)
- FINISHED EXERCISES (11:38am on 4/21/2009)
- Homework (p.115)
- HW-part 1: Migration: Add another column called "credit_card" to the users table as following as part of ActiveRecord_Creation project. (Or you can create a new project and new database. I will call the new project as MyActiveRecord here.) The type of credit_card field is string. (4/21/09: done, HINT ON PAGE 76) OUTPUT: (in C:/cygwin/home/jasnow/RAILSPASSION/ActiveRecord_Creation)
== 20090421151649 RenameEmailField: migrating =================================
-- rename_column(:users, :email, :email_addr)
-> 0.2011s
== 20090421151649 RenameEmailField: migrated (0.2018s) ========================
== 20090421162301 AddCreditCxard: migrating ===================================
-- add_column(:users, :credit_card, :string)
-> 0.1927s
== 20090421162301 AddCreditCxard: migrated (0.1933s) ==========================
- HW9-part 2: Validation: Add the following validation logic for the credit_card field.
- It has to be unique.
- User can enter the credit card format in the following format. The x has to be numeric value.
- xxxx-xxxx-xxxx-xxxx (format #1)
- xxxx xxxx xxxx xxxx (format #2)
- xxxxxxxxxxxxxxxx (format #3) NOTE: 16 digits
- HW9-part3: Callbacks: Use callbacks - before_save and before_create - to convert the format #1 and format #2 to format #3 meaning the actual table contains only format #3. (4/21/09: done, HINT ON PAGE 108) Loading development environment (Rails 2.1.0)
RAILS CONSOLE: JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
>> ?> user = User.new(:name => "OnlyDigits", :hobby => "tennis", :age => 49, :email_addr => "jasnow@hotmail.com", :credit_card => "1234567890123456" )
=> #
>> user.save
=> true
>> user = User.new(:name => "Digits N. Blanks", :hobby => "swimming", :age => 19, :email_addr => "dnbxxx@gmail.com", :credit_card => "1234 5678 9012 3456" )
=> #
>> user.save
=> true
>>
User.create(:name => "Digits N. Dashes", :hobby => "swimming", :age => 19, :email_addr => "dndxxx@gmail.com", :credit_card => "1234-5678-9012-3456" )
?> => #
>> User.create(:id => 20, :name => "Bad DashInColumn1", :hobby => "swimming", :age => 19, :email_addr => "bdc1xxx@gmail.com", :credit_card => "-1234567890123456" )
=> #
>> user = User.new(:name => "Bad DashInColumn1", :hobby => "swimming", :age => 19, :email_addr => "bdc1xxx@gmail.com", :credit_card => "-1234567890123456" )
=> #
>> user.save
=> false
>>
?> user = User.new(:name => "BadDashInColumn1", :hobby => "swimming", :age => 19, :email_addr => "bdc1xxx@gmail.com", :credit_card => "-1234567890123456" )
=> #
>> ?> user.save
=> false
>> user = User.new(:name => "BadValue", :hobby => "swimming", :age => 19, :email_addr => "bdc1xxx@gmail.com", :credit_card => "BAD VALUE" )
=> #
>> user.save
=> false - HW9-part4: Submit: Send the following files to railshomeworks@sun.com with Subject as RailsHomework-rails_activerecord.
- Zip file of the the MyActiveRecord NetBeans project. (Someone else should be able to open and run it as a NetBeans project.) You can use your favorite zip utility or you can use "jar" utility that comes with JDK as following.
- cd MyActiveRecord directory> (assuming you named your project as MyActiveRecord)
- jar cvf MyActiveRecord.zip MyActiveRecord (MyActiveRecord should contain nbproject directory)
- Captured output screen - name it as RailsHomework-rails_activerecord.gif orRailsHomework-rails_activerecord.jpg (or RailsHomework-rails_activerecord.)
- Any screen capture that shows that your program is working is good enough.
- Emailed on 4/21/2009/DONE with your lesson.
No comments:
Post a Comment