Sunday, April 19, 2009

Week 9: Rails Passion Course

  1. 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)
  2. Week 9 Resources
    1. Basics
      1. ActiveRecord from api.rubyonrails.org
      2. ActiveRecord::Base API from api.rubyonrails.org
      3. ActiveRecord tutorial screencast by Gregg (July 2007)
      4. Advanced ActiveRecord presentation by Obie Fernandez
    2. Validation
      1. ActiveRecord::Validations::ClassMethods API document from rubyonrails.com
      2. Various examples from railsbrains.com
    3. Migration
      1. ActiveRecord::Migration API document from railsbrains.com
      1. Generate a migration from an existing MySQL database posting in ruby-forum.com
  3. 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)
  4. Presentation --PDF: 1 slide per page (OR) OpenOffice file (44 pages) (4/21/09: Read)
  5. Online Lab/Change Log document (This is the same document you will find in the hands-on lab zip file) (105 pages)
  6. EXERCISE #0: Start MySQL database server. (p.2)
  7. EXERCISE #1: Creation of ActiveRecord Objects (p.2-39)
  8. (1.1) Create a new NetBeans project (with JRuby 1.1.4 and WEBrick) (p.3-6)
  9. (1.2) Create database (p.6-7)
  10. (1.3) Generate Model (3 attributes) (p.8-10)
  11. (1.4) Create database table through migration (p.10-19)
  12. (1.5) Create ActiveRecord objects through Rails Console (p.20-28)
  13. (1.6) Create database table rows through migration (p.29-39)
  14. EXERCISE #2: ActiveRecord#find (p.40-44)
  15. (2.1) find(:all) with various options (p.40-42)
  16. (2.1.1) With limit, offset, select options (p.40)
  17. (2.1.2) With order parameter (p.40-41)
  18. (2.1.3) With conditions parameter (p.41-42)
  19. (2.2) find(:first) (p.42)
  20. (2.3) find() with id (p.42)
  21. (2.4) Dynamic attribute-based finders (p.42-43)
  22. (2.5) find_by_sql method (p.43)
  23. (2.6) find_or_create_by_ method (p.43-44)
  24. (2.7) find() with params (44) -- EMPTY/SKIPPED
  25. EXERCISE #3: Validation (p.44-75)
  26. (3.1) Delete all rows of the "users" table through migration (p.44-49)
  27. (3.2) Perform validation using validation helpers (p.50-60)
  28. (3.3) Add email field to the users table (p.61-69)
  29. (3.4) Add validation logic for the "email" field (p.70-71)
  30. (3.5) Perform validation testing on the email field (p.72-75)
  31. EXERCISE #4: Migrations (p.76-105)
  32. (4.1) Add a new column (p.76)
  33. (4.2) Rename a column - chane "email" to "email_addr" (p.76-83)
  34. (4.3) Roll back the change - change "email_addr" to "email" (p.84-89)
  35. (4.4) Display "schema.rb" file that maintains the current migration number (p.90-91)
  36. (4.5) Exercise with multiple veresions of migration files (p.92-105)
  37. EXERCISE #5: Statistics (p.106-107)
  38. EXERCISE #6: Callbacks (p.108--114)
  39. (6.1) Add callback to the Model (p.108-114)
  40. FINISHED EXERCISES (11:38am on 4/21/2009)
  41. Homework (p.115)
  42. 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) ==========================
  43. HW9-part 2: Validation: Add the following validation logic for the credit_card field.
    1. It has to be unique.
    2. User can enter the credit card format in the following format. The x has to be numeric value.
      1. xxxx-xxxx-xxxx-xxxx (format #1)
      2. xxxx xxxx xxxx xxxx (format #2)
      3. xxxxxxxxxxxxxxxx (format #3) NOTE: 16 digits
  44. 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
  45. 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