ruby sapporo night vol3

Post on 10-May-2015

3.311 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Ruby札幌 presents

RubySapporoNight vol.3日本Rubyの会島田浩二

snoozer.05@gmail.comAppleStoreSapporo 2007-10-31(水)

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

しまだこうじ(snoozer.05)✓ 普通のフリーランス・プログラマ✓ ʻ06に独立し、武者修行中

✓ Rubyとの関わり✓ Ruby札幌運営✓ RubyKaigi2007当日スタッフ✓ るびま、るりま

✓ プログラミングの楽しさを皆さんと共有したい✓ 自分の知らないコトやヒトと出会いたい✓ http://iddy.jp/profile/snoozer05

本日もどうぞ宜しくお願いします

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

Ruby

Rubyとは✓ まつもとゆきひろ氏によって開発された動的オブジェクト指向スクリプト言語

✓ http://www.ruby-lang.org/ja/

✓ 楽しく、簡単に、自然にプログラミングがコンセプト

✓ プログラマにオープン✓ 柔軟かつアジャイルな開発を支援✓ A Programmerʼs Best Friend

Rubyとは

✓ インストールガイド- http://www.ruby-lang.org/ja/install.cgi

✓ チュートリアル- http://www.ruby-lang.org/ja/man/ ✓ リファレンスマニュアル

- http://www.ruby-lang.org/ja/man/ 

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

Ruby札幌

Ruby札幌とは✓ 札幌にてRubyに関するイベントや活動を行っているコミュニティ✓ “From Sapporo, With Love For Ruby.” ✓ Ruby勉強会@札幌✓ RubySapporoNight ←いまこれ✓ 開発集会@札幌✓ Ruby&Rails入門講座 

http://ruby-sapporo.org/

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

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 

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

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

いろいろとハマりどころもありそう

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

もうちょっとしたら情報もまとまるはず

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

ActiveRecordRuby on Rails 入門

ActiveRecord概要

ActiveRecordとは

Railsを構成する主要なライブラリの一つ

Railsが実現しているMVCモデルのM

何をするライブラリか

ORMObject / Relational Mapping

“オブジェクト指向言語におけるオブジェクトと、リレーショナルデータベース(RDB)におけるレコードとを対応づけること。ORMによって、RDBのレコードがプログラム上のオブジェクトとして直感的に扱えるようになり、RDBにアクセスするプログラムの記述を容易にすることが可能となる” - IT用語辞典より

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.”

まとめると

ActiveRecordとは✓ Railsを構成する主要なライブラリの一つ✓ MVCのMの部分✓ O/Rマッパ✓ P of EAA ʻActive Recordʼの実装✓ 簡単且つ効率的にRDB上の情報をRubyプログラム上のオブジェクトとして扱うことが可能

RailsRuby on

ActiveRecord概要

ARの基礎

DBへの接続

共有コネクタを定義しアダプタ経由で接続

ActiveRecord::Base.establish_connection( :adapter => “mysql”, :host => “localhost”, :username => “foo”, :password => “bar”, :database => “rsn”)

ORMの実現

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));

テーブル操作(CRUD)

レコードの生成(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”)

レコードの取得(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”)

レコードの取得(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”)

レコードの更新(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”)

レコードの削除(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) #関連レコードも削除

妥当性の確認

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

関連

1対1

loginemail

User user_idfirstnamelastnamebirthday

Profile

11

モデル定義

class User < ActiveRecord::Base has_one :profileend

class Profile < ActiveRecord::Base belongs_to :userend

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

1対多

nameaddress

Site titledescriptiontimesite_id

Event

*1

モデル定義

class Site < ActiveRecord::Base has_many :eventsend

class Event < ActiveRecord::Base belongs_to :siteend

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

多対多

event_iduser_idcreated_atupdated_at

Participation

Event User*1 1*

モデル定義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

多対多(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

コールバック

各ライフサイクルごとにRDBに依存しないコールバックの登録が可能

class User < ActiveRecord::Base before_save :write_debug_log

def write_debug_log ... endend

トランザクション

データベースレベルおよびオブジェクトレベルでのトランザクションをサポート

Site.transaction do Site.delete_all Event.delete_allend

AR基礎

✓ DBの接続✓ テーブル操作(CRUD)✓ 妥当性のルールの記述✓ モデル間の関連の定義の仕方✓ コールバック✓ トランザクション

RailsRuby on

ARの基礎

ちょっと先へ

更新や作成の記録

created_at, created_on というカラムを定義しておけば、

エントリ作成時に時刻が自動で入る

updated_at, updated_on というカラムを定義しておけば、

エントリ更新時に時刻が自動で入る

継承

typeというカラムを用意して一つのテーブルをみんなで共有(STI)

class Company < ActiveRecord::Base; endclass Firm < Company; endclass Client < Company; endclass PriorityClient < Company; end

バリューオブジェクト

複数のカラムをまとめて一つのオブジェクトにまとめる

class Customer < ActiveRecord::Base composed_of :name, :class_name => Name, :mapping => [[:first_name, :first], [:last_name, :last]]end

customer.name.first

論理削除

http://ar-paranoid.rubyforge.org/acts_as_paranoid

“delete_at”というカラムをdatetime型で定義して、モデルに以下を記述

class User < ActiveRecord::Base acts_as_paranoidend

オブザーバの導入

AR::Observerを継承した監視したいモデルのクラス名に基づいた

Observerを用意する

class UserObserver < ActiveRecord::Observer def after_create ... endend

ツリー構造

acts_as_treehttp://wiki.rubyonrails.org/rails/pages/ActsAsTree

“parent_id”というカラムを定義して、モデルに以下を記述する

class Category < ActiveRecord::Base acts_as_tree :order => “name”end

リスト構造

acts_as_list

以下のような感じで定義

class Category < ActiveRecord::Base has_many :items, :order => :nameend

class Item < ActiveRecord::Base belongs_to :category acts_as_list :scope => :categoryend

ちょっと先へ

さらにその先へ

複合キーを扱いたい

http://compositekeys.rubyforge.org

Composite Primary Keys Plugin

自然キーを扱いたい

主キーを明示的に定義

class Item < ActiveRecord::Base set_primary_key :item_idend

複数形でないテーブル名を使用したい

テーブル名を明示的に定義

class Item < ActiveRecord::Base set_table_name :item_stockend

バッチ処理でもARを

http://d.hatena.ne.jp/mrkn/20071010

ここを見ると良いと思うよ

サンプル

さらにその先へ

復習はこれらの本で

ARを使いこなして幸せなRailsライフを

ActiveRecordRuby on Rails 入門

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

予告

Ruby勉強会@札幌 第6回✓ 時:2007年11月17日(土)✓ 場所:北海道情報大学札幌サテライト✓ 参加費:無料 ✓ 内容:✓ C言語でRubyプログラムを作ろう✓ デプロイリベンジ✓ 開発集会体験記「あ...ありのまま(ry」

RubySapporoNight vol.4

✓ 時:2007年11月28日(水)✓ 場所:アップルストア札幌✓ 参加費:無料 ✓ 内容:✓ Ruby on Rails入門「???」✓ 何しようかなぁ

アジェンダ✓ 自己紹介✓ Rubyについて✓ Ruby札幌について✓ 速報「LeopardのRubyサポート状況」✓ Ruby on Rails入門「Active Record」✓ 予告

ご清聴ありがとうございました

何かご質問は?

top related