ruby sapporo night vol3
TRANSCRIPT
![Page 2: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/2.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 3: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/3.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 4: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/4.jpg)
しまだこうじ(snoozer.05)✓ 普通のフリーランス・プログラマ✓ ʻ06に独立し、武者修行中
✓ Rubyとの関わり✓ Ruby札幌運営✓ RubyKaigi2007当日スタッフ✓ るびま、るりま
✓ プログラミングの楽しさを皆さんと共有したい✓ 自分の知らないコトやヒトと出会いたい✓ http://iddy.jp/profile/snoozer05
![Page 7: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/7.jpg)
http://noplans.org/product/t-shirt/
![Page 8: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/8.jpg)
本日もどうぞ宜しくお願いします
![Page 9: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/9.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 10: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/10.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 11: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/11.jpg)
Ruby
![Page 12: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/12.jpg)
Rubyとは✓ まつもとゆきひろ氏によって開発された動的オブジェクト指向スクリプト言語
✓ http://www.ruby-lang.org/ja/
✓ 楽しく、簡単に、自然にプログラミングがコンセプト
✓ プログラマにオープン✓ 柔軟かつアジャイルな開発を支援✓ A Programmerʼs Best Friend
![Page 13: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/13.jpg)
Rubyとは
✓ インストールガイド- http://www.ruby-lang.org/ja/install.cgi
✓ チュートリアル- http://www.ruby-lang.org/ja/man/ ✓ リファレンスマニュアル
- http://www.ruby-lang.org/ja/man/
![Page 14: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/14.jpg)
Rubyとは
✓ 「PHPer向けRubyの基礎」 - http://www.slideshare.net/masuidrive/
ruby-90109
✓ 「JavaプログラマのためのRuby入門」 - http://www.kbmj.com/tech/index.php?
itemid=25
![Page 15: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/15.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 16: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/16.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 17: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/17.jpg)
Ruby札幌
![Page 18: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/18.jpg)
Ruby札幌とは✓ 札幌にてRubyに関するイベントや活動を行っているコミュニティ✓ “From Sapporo, With Love For Ruby.” ✓ Ruby勉強会@札幌✓ RubySapporoNight ←いまこれ✓ 開発集会@札幌✓ Ruby&Rails入門講座
![Page 20: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/20.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 21: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/21.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 22: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/22.jpg)
http://www.apple.com/jp/macosx/
祝
![Page 23: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/23.jpg)
http://www.chadfowler.cm/2007/10/28/ruby-on-leopard
“One of the most exciting features for me as a Rubyist is the inclusion of a well Ruby
installation.” - Chad Fowler
![Page 24: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/24.jpg)
Ruby and Leopard
✓ Ruby : 1.8.6 p36 ✓ RubyGems : 0.9.4 ✓ Ruby on Rails : 1.2.3 ✓ RubyCocoa : 1.0.0 ✓ XCode and Interface Builder ✓ Capistrano, Mongorel
![Page 25: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/25.jpg)
詳しくは
http://tarc.macosforge.org/projects/ruby/wiki/WhatsNewInLeopard
![Page 26: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/26.jpg)
http://gilesbowkett.blogspot.com/2007/10/apples-ruby-communities-not-features
“Apple’s latest OS ships with an improved Ruby, but it’s not an un-crippled Ruby - it’s just
a less-crippled Ruby.” - Giles Bowkett
![Page 27: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/27.jpg)
http://gilesbowkett.blogspot.com/2007/10/apples-ruby-communities-not-features
“Apple’s latest OS ships with an improved Ruby, but it’s not an un-crippled Ruby - it’s just
a less-crippled Ruby.” - Giles Bowkett
いろいろとハマりどころもありそう
![Page 28: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/28.jpg)
http://gilesbowkett.blogspot.com/2007/10/apples-ruby-communities-not-features
“Apple’s latest OS ships with an improved Ruby, but it’s not an un-crippled Ruby - it’s just
a less-crippled Ruby.” - Giles Bowkett
もうちょっとしたら情報もまとまるはず
![Page 29: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/29.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 30: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/30.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 31: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/31.jpg)
ActiveRecordRuby on Rails 入門
![Page 32: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/32.jpg)
ActiveRecord概要
![Page 33: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/33.jpg)
ActiveRecordとは
![Page 34: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/34.jpg)
Railsを構成する主要なライブラリの一つ
![Page 35: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/35.jpg)
Railsが実現しているMVCモデルのM
![Page 36: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/36.jpg)
何をするライブラリか
![Page 37: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/37.jpg)
ORMObject / Relational Mapping
![Page 38: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/38.jpg)
“オブジェクト指向言語におけるオブジェクトと、リレーショナルデータベース(RDB)におけるレコードとを対応づけること。ORMによって、RDBのレコードがプログラム上のオブジェクトとして直感的に扱えるようになり、RDBにアクセスするプログラムの記述を容易にすることが可能となる” - IT用語辞典より
![Page 39: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/39.jpg)
Active Record“An object that wraps a row in a database table or view, encapsules the database access, and
adds domain logic on that data.”
![Page 40: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/40.jpg)
まとめると
![Page 41: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/41.jpg)
ActiveRecordとは✓ Railsを構成する主要なライブラリの一つ✓ MVCのMの部分✓ O/Rマッパ✓ P of EAA ʻActive Recordʼの実装✓ 簡単且つ効率的にRDB上の情報をRubyプログラム上のオブジェクトとして扱うことが可能
RailsRuby on
![Page 42: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/42.jpg)
ActiveRecord概要
![Page 43: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/43.jpg)
ARの基礎
![Page 44: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/44.jpg)
DBへの接続
![Page 45: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/45.jpg)
共有コネクタを定義しアダプタ経由で接続
ActiveRecord::Base.establish_connection( :adapter => “mysql”, :host => “localhost”, :username => “foo”, :password => “bar”, :database => “rsn”)
![Page 46: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/46.jpg)
ORMの実現
![Page 47: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/47.jpg)
AR::Baseを継承してクラスをつくるとクラス名の複数形のテーブルに自動で
マッピングしてくれる
class User < ActiveRecord::Base ;end
CREATE TABLE users( id int(11) NOT NULL auto_increment, login varchar(255), email varchar(255), PRIMARY KEY(id));
![Page 48: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/48.jpg)
テーブル操作(CRUD)
![Page 49: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/49.jpg)
レコードの生成(CRUD)AR#save event = Event.new(:name => “RSN #1”)event.save
event = Event.newevent.name = “RSN #2”event.save
AR.create Event.create(:name => “RSN #3”)
![Page 50: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/50.jpg)
レコードの取得(CRUD)AR.find events = Event.find(:all)event = Event.find(:first)event = Event.find(1)event = Event.find(:first, [“name = ?”, “RSN #3”])
AR.find_by_xxx events = Event.find_by_name(“RSN #3”)
![Page 51: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/51.jpg)
レコードの取得(CRUD)
AR.find_by_xxx_and_yyy events = Event.find_by_id_and_name(1, “RSN #3”)
AR.find_or_create_by_xxx events = Event.find_or_create_by_name(“RSN”)
![Page 52: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/52.jpg)
レコードの更新(CRUD)AR#save event = Event.find(1)event.name = “foo”event.saveAR#update_attribute event.update_attribute(:name, “var”)AR#update_attributes event.update_attributes(:name=>“baz”,..)AR.update Event.update(1, :name=>“RSN vol1”)
![Page 53: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/53.jpg)
レコードの削除(CRUD)AR#destroy event = Event.find(1)event.destroyAR.delete Event.delete(2)AR.delete_all Event.delete_all(“id < 2”)AR.destroy Event.destroy(3) #関連レコードも削除
![Page 54: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/54.jpg)
妥当性の確認
![Page 55: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/55.jpg)
DSLっぽくかける
class User < ActiveRecord::Base validates_presence_of :name, :email,... validates_uniqueness_of :name validates_acceptance_of :terms_of_service, :on => create validates_comfirmation_of :password, :on => createend
![Page 56: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/56.jpg)
![Page 57: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/57.jpg)
関連
![Page 58: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/58.jpg)
1対1
loginemail
User user_idfirstnamelastnamebirthday
Profile
11
![Page 59: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/59.jpg)
モデル定義
class User < ActiveRecord::Base has_one :profileend
class Profile < ActiveRecord::Base belongs_to :userend
![Page 60: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/60.jpg)
1対1(One-to-One)所有者側(has_one) user = User.find(:first)user.profileuser.profile.nil?user.create_profile(:firstname => “foo”)
所属側(belongs_to)prof = Profile.find(:first)prof.create_user(:login => “snoozer.05”)prof.user.nil?prof.user = user
![Page 61: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/61.jpg)
1対多
nameaddress
Site titledescriptiontimesite_id
Event
*1
![Page 62: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/62.jpg)
モデル定義
class Site < ActiveRecord::Base has_many :eventsend
class Event < ActiveRecord::Base belongs_to :siteend
![Page 63: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/63.jpg)
1対多(One-to-Many)所有者側(has_many) apple_store = Site.find(:first)apple_store.eventsapple_store.events.create(:name => “RSN”)apple_store.events << event
所属側(belongs_to)rsn = Event.find(1)rsn.site.nil?rsn.site = apple_store
![Page 64: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/64.jpg)
多対多
event_iduser_idcreated_atupdated_at
Participation
Event User*1 1*
![Page 65: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/65.jpg)
モデル定義class User < ActiveRecord::Base has_many :participations has_many :register_events, :through => :participations, :source => eventend
class Participation < ActiveRecord::Base belongs_to :user belongs_to :eventend
class Event < ActiveRecord::Base has_many :participations has_many :members, :through => :participations, :source => userend
![Page 66: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/66.jpg)
多対多(Many-to-Many)所有者側(has_many) user.register_events user.register_events.nil?user.register_events << event
所有者側(has_many)event.members event.members.nil?event.members << user
![Page 67: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/67.jpg)
コールバック
![Page 68: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/68.jpg)
各ライフサイクルごとにRDBに依存しないコールバックの登録が可能
class User < ActiveRecord::Base before_save :write_debug_log
def write_debug_log ... endend
![Page 69: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/69.jpg)
トランザクション
![Page 70: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/70.jpg)
データベースレベルおよびオブジェクトレベルでのトランザクションをサポート
Site.transaction do Site.delete_all Event.delete_allend
![Page 71: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/71.jpg)
AR基礎
✓ DBの接続✓ テーブル操作(CRUD)✓ 妥当性のルールの記述✓ モデル間の関連の定義の仕方✓ コールバック✓ トランザクション
RailsRuby on
![Page 72: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/72.jpg)
ARの基礎
![Page 73: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/73.jpg)
ちょっと先へ
![Page 74: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/74.jpg)
更新や作成の記録
![Page 75: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/75.jpg)
created_at, created_on というカラムを定義しておけば、
エントリ作成時に時刻が自動で入る
![Page 76: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/76.jpg)
updated_at, updated_on というカラムを定義しておけば、
エントリ更新時に時刻が自動で入る
![Page 77: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/77.jpg)
継承
![Page 78: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/78.jpg)
typeというカラムを用意して一つのテーブルをみんなで共有(STI)
class Company < ActiveRecord::Base; endclass Firm < Company; endclass Client < Company; endclass PriorityClient < Company; end
![Page 79: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/79.jpg)
バリューオブジェクト
![Page 80: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/80.jpg)
複数のカラムをまとめて一つのオブジェクトにまとめる
class Customer < ActiveRecord::Base composed_of :name, :class_name => Name, :mapping => [[:first_name, :first], [:last_name, :last]]end
customer.name.first
![Page 81: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/81.jpg)
論理削除
![Page 82: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/82.jpg)
http://ar-paranoid.rubyforge.org/acts_as_paranoid
![Page 83: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/83.jpg)
“delete_at”というカラムをdatetime型で定義して、モデルに以下を記述
class User < ActiveRecord::Base acts_as_paranoidend
![Page 84: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/84.jpg)
オブザーバの導入
![Page 85: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/85.jpg)
AR::Observerを継承した監視したいモデルのクラス名に基づいた
Observerを用意する
class UserObserver < ActiveRecord::Observer def after_create ... endend
![Page 86: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/86.jpg)
ツリー構造
![Page 87: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/87.jpg)
acts_as_treehttp://wiki.rubyonrails.org/rails/pages/ActsAsTree
![Page 88: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/88.jpg)
“parent_id”というカラムを定義して、モデルに以下を記述する
class Category < ActiveRecord::Base acts_as_tree :order => “name”end
![Page 89: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/89.jpg)
リスト構造
![Page 90: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/90.jpg)
acts_as_list
![Page 91: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/91.jpg)
以下のような感じで定義
class Category < ActiveRecord::Base has_many :items, :order => :nameend
class Item < ActiveRecord::Base belongs_to :category acts_as_list :scope => :categoryend
![Page 92: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/92.jpg)
ちょっと先へ
![Page 93: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/93.jpg)
さらにその先へ
![Page 94: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/94.jpg)
複合キーを扱いたい
![Page 95: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/95.jpg)
http://compositekeys.rubyforge.org
Composite Primary Keys Plugin
![Page 96: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/96.jpg)
自然キーを扱いたい
![Page 97: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/97.jpg)
主キーを明示的に定義
class Item < ActiveRecord::Base set_primary_key :item_idend
![Page 98: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/98.jpg)
複数形でないテーブル名を使用したい
![Page 99: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/99.jpg)
テーブル名を明示的に定義
class Item < ActiveRecord::Base set_table_name :item_stockend
![Page 100: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/100.jpg)
バッチ処理でもARを
![Page 101: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/101.jpg)
http://d.hatena.ne.jp/mrkn/20071010
ここを見ると良いと思うよ
![Page 102: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/102.jpg)
サンプル
![Page 103: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/103.jpg)
さらにその先へ
![Page 104: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/104.jpg)
Lolcats 'n' Funny Pictures
![Page 105: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/105.jpg)
復習はこれらの本で
![Page 106: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/106.jpg)
ARを使いこなして幸せなRailsライフを
![Page 107: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/107.jpg)
ActiveRecordRuby on Rails 入門
![Page 108: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/108.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 109: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/109.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 110: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/110.jpg)
予告
![Page 111: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/111.jpg)
Ruby勉強会@札幌 第6回✓ 時:2007年11月17日(土)✓ 場所:北海道情報大学札幌サテライト✓ 参加費:無料 ✓ 内容:✓ C言語でRubyプログラムを作ろう✓ デプロイリベンジ✓ 開発集会体験記「あ...ありのまま(ry」
![Page 112: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/112.jpg)
RubySapporoNight vol.4
✓ 時:2007年11月28日(水)✓ 場所:アップルストア札幌✓ 参加費:無料 ✓ 内容:✓ Ruby on Rails入門「???」✓ 何しようかなぁ
![Page 113: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/113.jpg)
アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告
![Page 114: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/114.jpg)
ご清聴ありがとうございました
![Page 115: Ruby Sapporo Night Vol3](https://reader038.vdocuments.mx/reader038/viewer/2022103000/554f47e9b4c905423f8b4a74/html5/thumbnails/115.jpg)
何かご質問は?