ݺߣ

ݺߣShare a Scribd company logo
Model
                Hamamatsurb#5 2011.07.13 @mackato




2011   7   13
2011   7   13
Wiki

                5


2011   7   13
2011   7   13
2011   7   13
2011   7   13
2011   7   13
Model




                               - Ruby on Rails Guides
                Rails Database Migrations: http://bit.ly/rrH5zQ
                Active Record Validations and Callbacks: http://bit.ly/qp5vkm
                Active Record Associations: http://bit.ly/n3exu9
                Active Record Query Interface: http://bit.ly/qaeazJ


2011   7   13
Version 1.9.2

                             Version 3.0.7

                .rvmrc
                rvm ruby-1.9.2-p180@rails-3_0_7



2011   7   13
GitHub


           git clone git://github.com/hamamatsu-rb/rails3dojo.git

           git checkout -b working 2-model_pre




2011   7   13
FactoryGirl(             )   Spork(   )


                           Gem?le




                           .rspec


                          Spork
                          spork --bootstrap

2011   7   13
Wiki


                User: Wiki
                Page:                  Wiki
                Comment: Page
                History: Page




2011   7   13
Wiki

                 rails g model user name:string

                 rake db:migrate




2011   7   13
spec/factories.rb




                spec/spec_helper.rb




2011   7   13
spec/models/user_spec.rb




2011   7   13
app/models/user.rb
                                           SexyValidation!




                 Legacy Style Validation




2011   7   13
bundle exec rspec spec/models/user_spec.rb
                        User
                          #name


                            50


                          #find_or_create_by_name




2011   7   13
Wiki

                rails g model page title:string body:text 
                 user_id:integer

                rake db:migrate




2011   7   13
spec/factories.rb




                spec/spec_helper.rb




2011   7   13
spec/models/page_spec.rb(   )




2011   7   13
app/models/page.rb




2011   7   13
Page

                rails g model comment body:text 
                 page_id:integer user_id:integer

                rake db:migrate




2011   7   13
spec/factories.rb




                spec/spec_helper.rb




2011   7   13
spec/models/comment_spec.rb(   )




2011   7   13
app/models/comment.rb




2011   7   13
spec/models/page_spec.rb (   )




                      app/models/page.rb




2011   7   13
Page

                rails g model history 
                page_id:integer user_id:integer

                rake db:migrate




2011   7   13
spec/factories.rb




                spec/spec_helper.rb




2011   7   13
spec/models/history_spec.rb




2011   7   13
app/models/history.rb




2011   7   13
spec/models/page_spec.rb (   )




                      app/models/page.rb




2011   7   13
spec/models/page_spec.rb (   )




                      app/models/page.rb




2011   7   13
master
                        git checkout master
                        git merge working
                        git branch -d working

                                     GitHub
           git clone git://github.com/hamamatsu-rb/rails3dojo.git
           git checkout 2-model


2011   7   13
2011   7   13
page
           db/migrate/20110712154821_create_pages.rb




                                                          rake db:migrate [VERSION=YY..]
                                                          rake db:rollback [STEP=N]
                                                          rake db:migrate:redo [STEP=N]




                                      page


2011   7    13
Validation Helpers   vs   SexyValidation




2011   7   13
The belongs_to Association                 The has_many :through Association

        class ?Order ?< ?ActiveRecord::Base
         ? ?belongs_to ?:customer               class ?Physician ?< ?ActiveRecord::Base
        end                                      ? ?has_many ?:appointments
                                                 ? ?has_many ?:patients, ?:through ?=> ?:appointments
                                                end
            The has_one Association              ?
                                                class ?Appointment ?< ?ActiveRecord::Base
       class ?Supplier ?< ?ActiveRecord::Base    ? ?belongs_to ?:physician
        ? ?has_one ?:account                     ? ?belongs_to ?:patient
       end                                      end
                                                 ?
                                                class ?Patient ?< ?ActiveRecord::Base
                                                 ? ?has_many ?:appointments
            The has_many Association             ? ?has_many ?:physicians, ?:through ?=> ?:appointments
                                                end
       class ?Customer ?< ?ActiveRecord::Base
        ? ?has_many ?:orders
       end



                                                    : Active Record Associations: http://bit.ly/n3exu9

2011    7    13
Finder methods             Retrieving a Single Object

                   ?   where
                   ?   select
                   ?   group
                   ?   order               Retrieving Multiple Objects
                   ?   limit
                   ?   offset
                   ?   joins
                   ?   includes
                   ?   lock                         Calculations
                   ?   readonly
                   ?   from
                   ?   having

                                  Active Record Query Interface: http://bit.ly/qaeazJ

2011   7   13
2011   7   13
2011   7   13
2011   7   13
2011   7   13

More Related Content

Rails3 ΏrModel

  • 1. Model Hamamatsurb#5 2011.07.13 @mackato 2011 7 13
  • 2. 2011 7 13
  • 3. Wiki 5 2011 7 13
  • 4. 2011 7 13
  • 5. 2011 7 13
  • 6. 2011 7 13
  • 7. 2011 7 13
  • 8. Model - Ruby on Rails Guides Rails Database Migrations: http://bit.ly/rrH5zQ Active Record Validations and Callbacks: http://bit.ly/qp5vkm Active Record Associations: http://bit.ly/n3exu9 Active Record Query Interface: http://bit.ly/qaeazJ 2011 7 13
  • 9. Version 1.9.2 Version 3.0.7 .rvmrc rvm ruby-1.9.2-p180@rails-3_0_7 2011 7 13
  • 10. GitHub git clone git://github.com/hamamatsu-rb/rails3dojo.git git checkout -b working 2-model_pre 2011 7 13
  • 11. FactoryGirl( ) Spork( ) Gem?le .rspec Spork spork --bootstrap 2011 7 13
  • 12. Wiki User: Wiki Page: Wiki Comment: Page History: Page 2011 7 13
  • 13. Wiki rails g model user name:string rake db:migrate 2011 7 13
  • 14. spec/factories.rb spec/spec_helper.rb 2011 7 13
  • 16. app/models/user.rb SexyValidation! Legacy Style Validation 2011 7 13
  • 17. bundle exec rspec spec/models/user_spec.rb User #name 50 #find_or_create_by_name 2011 7 13
  • 18. Wiki rails g model page title:string body:text user_id:integer rake db:migrate 2011 7 13
  • 19. spec/factories.rb spec/spec_helper.rb 2011 7 13
  • 22. Page rails g model comment body:text page_id:integer user_id:integer rake db:migrate 2011 7 13
  • 23. spec/factories.rb spec/spec_helper.rb 2011 7 13
  • 26. spec/models/page_spec.rb ( ) app/models/page.rb 2011 7 13
  • 27. Page rails g model history page_id:integer user_id:integer rake db:migrate 2011 7 13
  • 28. spec/factories.rb spec/spec_helper.rb 2011 7 13
  • 31. spec/models/page_spec.rb ( ) app/models/page.rb 2011 7 13
  • 32. spec/models/page_spec.rb ( ) app/models/page.rb 2011 7 13
  • 33. master git checkout master git merge working git branch -d working GitHub git clone git://github.com/hamamatsu-rb/rails3dojo.git git checkout 2-model 2011 7 13
  • 34. 2011 7 13
  • 35. page db/migrate/20110712154821_create_pages.rb rake db:migrate [VERSION=YY..] rake db:rollback [STEP=N] rake db:migrate:redo [STEP=N] page 2011 7 13
  • 36. Validation Helpers vs SexyValidation 2011 7 13
  • 37. The belongs_to Association The has_many :through Association class ?Order ?< ?ActiveRecord::Base ? ?belongs_to ?:customer class ?Physician ?< ?ActiveRecord::Base end ? ?has_many ?:appointments ? ?has_many ?:patients, ?:through ?=> ?:appointments end The has_one Association ? class ?Appointment ?< ?ActiveRecord::Base class ?Supplier ?< ?ActiveRecord::Base ? ?belongs_to ?:physician ? ?has_one ?:account ? ?belongs_to ?:patient end end ? class ?Patient ?< ?ActiveRecord::Base ? ?has_many ?:appointments The has_many Association ? ?has_many ?:physicians, ?:through ?=> ?:appointments end class ?Customer ?< ?ActiveRecord::Base ? ?has_many ?:orders end : Active Record Associations: http://bit.ly/n3exu9 2011 7 13
  • 38. Finder methods Retrieving a Single Object ? where ? select ? group ? order Retrieving Multiple Objects ? limit ? offset ? joins ? includes ? lock Calculations ? readonly ? from ? having Active Record Query Interface: http://bit.ly/qaeazJ 2011 7 13
  • 39. 2011 7 13
  • 40. 2011 7 13
  • 41. 2011 7 13
  • 42. 2011 7 13