浜松Rails3道場 其の弐 Model編

  • 1,344 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,344
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
17
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Model Hamamatsurb#5 2011.07.13 @mackato2011 7 13
  • 2. 2011 7 13
  • 3. Wiki 52011 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/qaeazJ2011 7 13
  • 9. Version 1.9.2 Version 3.0.7 .rvmrc rvm ruby-1.9.2-p180@rails-3_0_72011 7 13
  • 10. GitHub git clone git://github.com/hamamatsu-rb/rails3dojo.git git checkout -b working 2-model_pre2011 7 13
  • 11. FactoryGirl( ) Spork( ) Gemfile .rspec Spork spork --bootstrap2011 7 13
  • 12. Wiki User: Wiki Page: Wiki Comment: Page History: Page2011 7 13
  • 13. Wiki rails g model user name:string rake db:migrate2011 7 13
  • 14. spec/factories.rb spec/spec_helper.rb2011 7 13
  • 15. spec/models/user_spec.rb2011 7 13
  • 16. app/models/user.rb SexyValidation! Legacy Style Validation2011 7 13
  • 17. bundle exec rspec spec/models/user_spec.rb User #name 50 #find_or_create_by_name2011 7 13
  • 18. Wiki rails g model page title:string body:text user_id:integer rake db:migrate2011 7 13
  • 19. spec/factories.rb spec/spec_helper.rb2011 7 13
  • 20. spec/models/page_spec.rb( )2011 7 13
  • 21. app/models/page.rb2011 7 13
  • 22. Page rails g model comment body:text page_id:integer user_id:integer rake db:migrate2011 7 13
  • 23. spec/factories.rb spec/spec_helper.rb2011 7 13
  • 24. spec/models/comment_spec.rb( )2011 7 13
  • 25. app/models/comment.rb2011 7 13
  • 26. spec/models/page_spec.rb ( ) app/models/page.rb2011 7 13
  • 27. Page rails g model history page_id:integer user_id:integer rake db:migrate2011 7 13
  • 28. spec/factories.rb spec/spec_helper.rb2011 7 13
  • 29. spec/models/history_spec.rb2011 7 13
  • 30. app/models/history.rb2011 7 13
  • 31. spec/models/page_spec.rb ( ) app/models/page.rb2011 7 13
  • 32. spec/models/page_spec.rb ( ) app/models/page.rb2011 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-model2011 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] page2011 7 13
  • 36. Validation Helpers vs SexyValidation2011 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/n3exu92011 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/qaeazJ2011 7 13
  • 39. 2011 7 13
  • 40. 2011 7 13
  • 41. 2011 7 13
  • 42. 2011 7 13