際際滷

際際滷Share a Scribd company logo
BP Study #15
 或檎マッパ`Q?2008


Active Recordとか
徭失B初


?   硫鍋 崘畔
?   ID:yuroyoro
?   フリ`エンジニア
?   Java、恷除はScala
Blog
或檎マッパ`
   Q
ってことで
20081128 Bp Study#15 Active Record
20081128 Bp Study#15 Active Record
20081128 Bp Study#15 Active Record
Active Record
ActiveRecordって?


?RailsでのO/R Mapper
?Railsなくてもつかえるけど
?ActiveRecordパタ`ン
?Mapping協xがg
?class Member < ActiveRecord::Base
ActiveRecordって?


?DDLvSはMigrationで
?Act_as_*で尅る玲いを辛嬬
?啜張侫.ぅ鵐
?Lazy Loading
?Named Scope -&乙岳;イカス
やってみよう!
ActiveRecordを聞う



?MySqlをインスト`ルしておく
?MySqlにDatabaseを恬っておく
?Railsアプリを伏撹する
?Migrationく
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
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
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)
     ===========================
ActiveRecordを聞う - 4
Consoleで_J
 $ 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 ̄>
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;>
ActiveRecordを聞う - 6
vBのO協
 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
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
ActiveRecord まとめ

よかったさがし

?Mapping協xはS
?Pluginいっぱい
?Lazy Loading
?Irb貧でg_J
ActiveRecord まとめ

Disってみる
Disってみる

?リファクタリング寄
?Migrationは鮫議に
?IDEのa頼が´
?Joinとかにがて
おまけ
Active Objects
ActiveObjectsって?
?ARパタ`ンのJavag廾
?Interfaceにgetter/setterで
?manager.migrate(Person.class);
?EntityManager
?アノテ`ション
?http://d.hatena.ne.jp/nattou_cur
ry_2/
ご賠
ありがとう
ございました

More Related Content

20081128 Bp Study#15 Active Record