20081128 Bp Study#15 Active Record

1,278 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,278
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

20081128 Bp Study#15 Active Record

  1. 1. BP Study #15 ORマッパー対決〜2008 Active Recordとか
  2. 2. 自己紹介 • 尾崎 智仁 • ID:yuroyoro • フリーエンジニア • Java、最近はScala
  3. 3. Blog
  4. 4. ORマッパー 対決 ってことで
  5. 5. Active Record
  6. 6. ActiveRecordって? •RailsでのO/R Mapper •Railsなくてもつかえるけど •ActiveRecordパターン •Mapping定義が簡単 •class Member < ActiveRecord::Base
  7. 7. ActiveRecordって? •DDL関係はMigrationで •Act_as_*で振る舞いを拡張可能 •動的ファインダ •Lazy Loading •Named Scope ->イカス
  8. 8. やってみよう!
  9. 9. ActiveRecordを使う準備 •MySqlをインストールしておく •MySqlにDatabaseを作っておく •Railsアプリを生成する •Migration書く
  10. 10. ActiveRecordを使う - 1 Railアプリを生成 $ rails ARTest create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments create config/initializers create db create doc
  11. 11. ActiveRecordを使う - 2 Modelを生成する $ ./script/generate model member username:string password:string ¥ sex:decimal birthday:datetime last_login_time:timestamp :0:Warning: Gem::SourceIndex#search support for Regexp patterns is deprecated exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/member.rb create test/unit/member_test.rb create test/fixtures/members.yml create db/migrate create db/migrate/20081127123415_create_members.rb
  12. 12. ActiveRecordを使う - 3 Migrateする $ rake db:migrate (in /Users/ozaki/dev/Project/sandbox/work/ARTest) == 20081127123415 CreateMembers: migrating ==================================== -- create_table(:members) -> 0.0049s == 20081127123415 CreateMembers: migrated (0.0054s) ===========================
  13. 13. ActiveRecordを使う - 4 Consoleで確認 $ script/console Loading development environment (Rails 2.1.0) >> member=Member.find(:all) => [] >> Member.create #<Member id: 1, username: nil, password: nil, first_name: nil, last_name: nil, sex: nil, birthday: nil, last_login_time: nil, created_at: quot;2008-11-27 13:16:47quot;, updated_at: quot;2008-11-27 13:16:47quot;> >> Member.find(1) => #<Member id: 1, username: nil, password: nil, first_name: nil, last_name: nil, sex: nil, birthday: nil, last_login_time: nil, created_at: quot;2008-11-27 13:16:47quot;, updated_at: quot;2008-11-27 13:16:47”>
  14. 14. ActiveRecordを使う - 5 動的な属性ベースのファインダ >> Member.find_by_username('Test2') => #<Member id: 2, username: quot;Test2quot;, password: nil, first_name: nil, last_name: nil, sex: nil, birthday: nil, last_login_time: nil, created_at: quot;2008-11-27 13:17:04quot;, updated_at: quot;2008-11- 27 13:21:14quot;>
  15. 15. ActiveRecordを使う - 6 関連の設定 class Employee< ActiveRecord::Base belongs_to :department has_many :roles ,:through => :belongs End class Department< ActiveRecord::Base has_many :employees acts_as_tree :order => quot;idquot; end
  16. 16. ActiveRecordを使う - 7 Named_scope class Member < ActiveRecord::Base named_scope :male, :conditions=>[quot;sex = ?quot;,1] named_scope :recent, lambda {|*args| {:conditions => [quot;updated_at > ?quot;, args.first || 1.day.ago]} } end
  17. 17. ActiveRecord まとめ よかったさがし •Mapping定義は楽 •Pluginいっぱい •Lazy Loading •Irb上で簡単確認
  18. 18. ActiveRecord まとめ Disってみる Disってみる •リファクタリング大変 •Migrationは計画的に •IDEの補完が… •Joinとかにがて
  19. 19. おまけ Active Objects
  20. 20. ActiveObjectsって? •ARパターンのJava実装 •Interfaceにgetter/setterで •manager.migrate(Person.class); •EntityManager •アノテーション •http://d.hatena.ne.jp/nattou_cur ry_2/
  21. 21. ご清聴 ありがとう ございました

×