狠狠撸

狠狠撸Share a Scribd company logo
2011/10/22
?   Okinawa.rb 第一回に参加した人???嘘
    ? 発表資料はこちら
     http://www.slideshare.net/yasulab/rails-tutorial-ch12
     (謝謝 @yasulab さん)
?   git が使える
?   rails 3.0 (最新は 3.1)
?   rails インストールして、scaffold して、r s で
    確認できる
? 「rails g scaffold blog …」で CRUD出来るの
  はもう知ってるよ
? 複数のモデル(テーブル)の連携部分を深く
  掘り下げて考える
? ActiveRecord::Associations の has_many
  なモデルのCRUD画面作成
? 「多対多」の関係(has_many :through)
    ? routing, controller, view
? 隙を見て rails.vim の話やその他もろもろ...
? 仕様を満たしていくまでの考え方、開発リズ
  ム
    ? つっこみ歓迎w
? 設計してみよう
? テニス(や卓球)のエントリ(申込)を表現
  してみよう

?   サンプルプログラムのソース
    ? git@github.com:naopontan/my_entry.git
? Aさんはログインして参加可能の大会の中から
  自分の好きな大会を選びます。
? 次に種目(シングルス or ダブルス)を選択し
  ます。
? ダブルスの場合はパートナー一覧からパート
  ナーを選びます
? 申込内容はいつでも確認できます
? Aさんはログインして参加可能の大会の中から
  自分の好きな大会を選びます。
? 次に種目(シングルス or ダブルス)を選択し
  ます。
? ダブルスの場合はパートナー一覧からパート
  ナーを選びます
? 申込内容はいつでも確認できます
? 1つの大会は複数の種目を持つ
? 1つの種目は複数の申込を持つ
? Userは複数の申込を持つ
? User と “種目”は 多対多の関係だよね
  ( user has_many events through => :entry)
大会




ユーザ
           種目




      申込
competitions
                              id
                              name



users
 id                         events
 name                        id
 is_male                     competition_id (FK)
                             name
           entries           is_male
            id
            event_id (FK)
            user_id (FK)
?   r new my_entry
    ? 要オプション確認: --skip-prototype, --skip-test-unit, etc…
?   cd my_entry
?   bundle install
?   git init
?   git add .
?   git commit –m ‘first commit’
?   rake db:migrate
?   r s して http://localhost:3000/ にアクセス
?   git rm public/index.html
?   r g scaffold Competition name:string
        ? config/routes.rb
        ? test/fixtures/competitions.yml
          (RSpec の場合は
          spec/fixtures/competions.yml)
    % cat test/fixtures/competitions
    one:
     name: 招き猫オープン

    two:
     name: 閑古鳥杯

?   rake db:migrate
?    r g scaffold Event competition_id:integer
     name:string is_male:boolean
     % cat test/fixtures/events
     one:
      competition_id: 1
      name: MyString
      is_male: false

     two:
      ...


?   「大会」と関連付けるため、上記を修正

?   boolean 型の注意点
    t.boolean :is_male, :null => false, :default => true
% cat test/fixtures/events   % cat app/model/event.rb
one:                         class Event < ActiveRecord::Base
 competition: one             belongs_to :competition
 name: 18歳以下男子シングルス          end
 is_male: true

two:
 competition: one
  name: 18歳以下女子シングルス
  is_male: false

three:
  ...

?   rake db:fixtures:load でテストデータを読み込み
?   テストデータが多すぎるのはNG
    ? 例えば users.yml は 3人~5人 ぐらいじゃね?
    ? テストプログラムでパターンが足りない場合は動
      的にテストデータ生成
?   rc
         ? Competition.all
         ? Event.all
         ? Event.first
         ? Event.all[0].competition
         ? Event.all[0].competition.event # ←エラー
    % cat app/model/competition.rb
    class Event < ActiveRecord::Base
     has_many :events
    end

?   competition has many events
     ? Event.all[0].competition.event # ←Good!
     ? Competition.find_by_name(‘閑古鳥杯’).events.empty?
? そもそも has_many や belong_to の実装前
  にテストから書く(ハズ)
? Event#competition_id は必須?
? 大会名は必須?名前の重複は変だよね?
? 種目名が1つの大会で重複もしかり
? 最大文字数とかは?
? ...ということで最初は validation のOK/NGの
  テストとかになるはず...
?   r g scaffold User name:string is_male:boolean
    ? db/migrate/*users_rb の boolean はnull, default を忘れ
      ずに
? fixutres は男性3人、女性1人ぐらいで
? 認証はプラグインで(devise, Sorcery, ...)
    ? 権限も今回は無し
? r g model Entry event_id:integer
  user_id:integer
? test/fixtures/entries.yml
     % cat app/model/entry.rb
     class Entry < ActiveRecord::Base
       belongs_to :event
       belongs_to :user              % cat app/model/user.rb
     end                             class User < ActiveRecord::Base
                                      has_many :entries
                                      has_many :events, :through => :entries
 % cat app/model/event.rb            end
 class Event < ActiveRecord::Base
  has_many :entries
  has_many :users, :through => :entries
 end
competitions
                              id
                              name



users
 id                         events
 name                        id
 is_male                     competition_id (FK)
                             name
           entries           is_male
            id
            event_id (FK)
            user_id (FK)
?   ログイン
    ?   大会一覧(種目一覧)
?   管理者                             ?   利用者
    ? 大会と種目の編集                          ? 申込み一覧(自分の
    ? 会員一覧                                み)
    ? 申込み一覧                             ? 申込み



         何から手をつけようか???
         大会は scaffold のままでほとんどOK。
? rake routes | grep events で基本を把握する
? competition has_many events を routes.rb で表現
   ? events を competition にぶら下げる
   ? そして、再度 rake routes | grep events

    * config/routes.rb *
    resources :users
    resources :competitions do
     resources :events, :except => [:index]
    end
    ?   種目の一覧って2通り考えられる?
        ? 1つの大会に着目したときの、それにぶら下がる一覧
        ? とにかく全て(大会の域を超えて)の一覧 ←本当に必要か?
?   ここでは実際のソースを見ながら解説します
    ? 以下、一部抜粋

    class EventsController < ApplicationController
     before_filter :find_competition

     def new
      @event = @competition.events.new
     end
?   competitions#show に種目の CRUD を挿入

<p>
 <b>Name:</b>
 <%= @competition.name %>
</p>

<table border="1">
 <tr>
   <th>Name</th>
   <th>Is male</th>
   <th></th>
   <th></th>
   <th></th>
 </tr>
 <%= render @competition.events %>
</table>
<%= link_to 'New Event', new_competition_event_path(@competition) %>
?   将来、権限機能を実装するのは明らかだ
    ? 基本、管理者は何でもできる
    ? ということは全てのUserの申込も操作できねば
    ? 申込の操作が実装できれば、後は「一般Userの申
      込も流用できそうだ」
?   …ということで(私は)申込を users#show
    に実装することに決めた
<table border="1">
 <tr>
   <th>Competition</th>
   <th>Event</th>
   <th></th>
 </tr>
 <% @user.entries.each do |entry| %>
 <tr>
   <td><%= entry.event.competition.name %></td>
   <td><%= entry.event.name %></td>
   <td><%= link_to 'Cancel', entry, :confirm => 'Are you sure?', :method => :delete %></td>
 </tr>
 <% end %>
</table>


<h2>申込</h2>
<%= form_tag(entries_path, :method => :get) do %>
 <%= ("competition", "id", Competition.all.collect {|| [ .name, .id ] }) %>
 <%= hidden_field_tag :user_id, @user.id %>
 <%= submit_tag '次へ' %>
<% end %>
competitions
                              id
                              name



users
 id                         events
 name                        id
 is_male                     competition_id (FK)
                             name
           entries           is_male
            id
            event_id (FK)
            user_id (FK)
? entry.event.competition... が冗長だ
? entry から competition は1つなのに...
? API 見ると delegate メソッド発見!

    class Entry < ActiveRecord::Base
     delegate :competition, :to => :event
     ...

    ? entry.competition って書けた。Happy!
    ? この辺りは経験とか勉強の度合いで感じ取る
      ? def competition; event.competition; end
Okinawa.rb 第2回勉強会
? ActiveRecord::Associations の has_many
  なモデルのCRUD画面作成
? 「多対多」の関係(has_many :through)
    ? routing, controller, view
? 隙を見て rails.vim の話やその他もろもろ...
? 仕様を満たしていくまでの考え方、開発リ
  ズム
    ? つっこみ歓迎w
Agile Web Development with Rails (4th edition)
http://pragprog.com/book/rails4/agile-web-development-with-rails

RailsによるアジャイルWebアプリケーション開発 第3版(注意!Rails2 です)
http://ssl.ohmsha.co.jp/cgi-bin/menu.cgi?ISBN=978-4-274-06785-3
? たくさんありすぎ…
? @IT自分戦略研究所
    http://jibun.atmarkit.co.jp/scenter/ittrain/
    (1日1回、RSSフィードで地味にやってます)
     ? ITトレメ Ruby技術者認定【Gold】試験
     ? ITトレメ Rails 3 技術者認定ブロンズ試験
Okinawa.rb 第2回勉強会

More Related Content

Similar to Okinawa.rb 第2回勉強会 (20)

nomlab_okayamaruby_subslide
nomlab_okayamaruby_subslide
nomlab
?
Hyper → Highspeed → Development
Hyper → Highspeed → Development
aktsk
?
Ruby on Rails Tutorial Chapter8-10
Ruby on Rails Tutorial Chapter8-10
Sea Mountain
?
Rails基礎講座 part.2
Rails基礎講座 part.2
Jun Yokoyama
?
Ruby on Rails 入門
Ruby on Rails 入門
Yasuko Ohba
?
nomlab_okayamaruby_slide
nomlab_okayamaruby_slide
nomlab
?
诲别惫颈蝉别を利用した认証について@惭颈苍补尘颈谤产
诲别惫颈蝉别を利用した认証について@惭颈苍补尘颈谤产
Jun Fukaya
?
リソースモデリングパターンの提案 #sendagayarb
リソースモデリングパターンの提案 #sendagayarb
Toru Kawamura
?
Next-L Enju 開発ワークショップ #4
Next-L Enju 開発ワークショップ #4
Kosuke Tanabe
?
【入门】3时间でアプリ公开!ゼロからのプログラミング搁补颈濒蝉讲座
【入门】3时间でアプリ公开!ゼロからのプログラミング搁补颈濒蝉讲座
DIVE INTO CODE Corp.
?
The Essence of Using Ruby on Rails in Corporations
The Essence of Using Ruby on Rails in Corporations
Koichiro Ohba
?
Next-L Enju 開発WS #03 Ruby on Railsの使い方
Next-L Enju 開発WS #03 Ruby on Railsの使い方
Kosuke Tanabe
?
Tdd
Tdd
Takaya Kotohata
?
Ruby on Rails Tutorial Chapter5-7
Ruby on Rails Tutorial Chapter5-7
Sea Mountain
?
搁补颈濒蝉ハイパー実践讲座-第35回狈补颁濒勉强会
搁补颈濒蝉ハイパー実践讲座-第35回狈补颁濒勉强会
Narihiro Nakamura
?
名前重要 超重要
名前重要 超重要
baban ba-n
?
はし?めての Rails アフ?リ開発
はし?めての Rails アフ?リ開発
n-fukidome
?
20110820 metaprogramming
20110820 metaprogramming
Masanori Kado
?
名古屋Ruby会議02 LT:Ruby中級への道
名古屋Ruby会議02 LT:Ruby中級への道
Shigeru UCHIYAMA
?
Rails Controller Fundamentals
Rails Controller Fundamentals
Takashi SAKAGUCHI
?
nomlab_okayamaruby_subslide
nomlab_okayamaruby_subslide
nomlab
?
Hyper → Highspeed → Development
Hyper → Highspeed → Development
aktsk
?
Ruby on Rails Tutorial Chapter8-10
Ruby on Rails Tutorial Chapter8-10
Sea Mountain
?
Rails基礎講座 part.2
Rails基礎講座 part.2
Jun Yokoyama
?
Ruby on Rails 入門
Ruby on Rails 入門
Yasuko Ohba
?
nomlab_okayamaruby_slide
nomlab_okayamaruby_slide
nomlab
?
诲别惫颈蝉别を利用した认証について@惭颈苍补尘颈谤产
诲别惫颈蝉别を利用した认証について@惭颈苍补尘颈谤产
Jun Fukaya
?
リソースモデリングパターンの提案 #sendagayarb
リソースモデリングパターンの提案 #sendagayarb
Toru Kawamura
?
Next-L Enju 開発ワークショップ #4
Next-L Enju 開発ワークショップ #4
Kosuke Tanabe
?
【入门】3时间でアプリ公开!ゼロからのプログラミング搁补颈濒蝉讲座
【入门】3时间でアプリ公开!ゼロからのプログラミング搁补颈濒蝉讲座
DIVE INTO CODE Corp.
?
The Essence of Using Ruby on Rails in Corporations
The Essence of Using Ruby on Rails in Corporations
Koichiro Ohba
?
Next-L Enju 開発WS #03 Ruby on Railsの使い方
Next-L Enju 開発WS #03 Ruby on Railsの使い方
Kosuke Tanabe
?
Ruby on Rails Tutorial Chapter5-7
Ruby on Rails Tutorial Chapter5-7
Sea Mountain
?
搁补颈濒蝉ハイパー実践讲座-第35回狈补颁濒勉强会
搁补颈濒蝉ハイパー実践讲座-第35回狈补颁濒勉强会
Narihiro Nakamura
?
名前重要 超重要
名前重要 超重要
baban ba-n
?
はし?めての Rails アフ?リ開発
はし?めての Rails アフ?リ開発
n-fukidome
?
20110820 metaprogramming
20110820 metaprogramming
Masanori Kado
?
名古屋Ruby会議02 LT:Ruby中級への道
名古屋Ruby会議02 LT:Ruby中級への道
Shigeru UCHIYAMA
?

Okinawa.rb 第2回勉強会

  • 2. ? Okinawa.rb 第一回に参加した人???嘘 ? 発表資料はこちら http://www.slideshare.net/yasulab/rails-tutorial-ch12 (謝謝 @yasulab さん) ? git が使える ? rails 3.0 (最新は 3.1) ? rails インストールして、scaffold して、r s で 確認できる
  • 3. ? 「rails g scaffold blog …」で CRUD出来るの はもう知ってるよ ? 複数のモデル(テーブル)の連携部分を深く 掘り下げて考える
  • 4. ? ActiveRecord::Associations の has_many なモデルのCRUD画面作成 ? 「多対多」の関係(has_many :through) ? routing, controller, view ? 隙を見て rails.vim の話やその他もろもろ... ? 仕様を満たしていくまでの考え方、開発リズ ム ? つっこみ歓迎w
  • 5. ? 設計してみよう ? テニス(や卓球)のエントリ(申込)を表現 してみよう ? サンプルプログラムのソース ? git@github.com:naopontan/my_entry.git
  • 6. ? Aさんはログインして参加可能の大会の中から 自分の好きな大会を選びます。 ? 次に種目(シングルス or ダブルス)を選択し ます。 ? ダブルスの場合はパートナー一覧からパート ナーを選びます ? 申込内容はいつでも確認できます
  • 7. ? Aさんはログインして参加可能の大会の中から 自分の好きな大会を選びます。 ? 次に種目(シングルス or ダブルス)を選択し ます。 ? ダブルスの場合はパートナー一覧からパート ナーを選びます ? 申込内容はいつでも確認できます
  • 8. ? 1つの大会は複数の種目を持つ ? 1つの種目は複数の申込を持つ ? Userは複数の申込を持つ ? User と “種目”は 多対多の関係だよね ( user has_many events through => :entry)
  • 9. 大会 ユーザ 種目 申込
  • 10. competitions id name users id events name id is_male competition_id (FK) name entries is_male id event_id (FK) user_id (FK)
  • 11. ? r new my_entry ? 要オプション確認: --skip-prototype, --skip-test-unit, etc… ? cd my_entry ? bundle install ? git init ? git add . ? git commit –m ‘first commit’ ? rake db:migrate ? r s して http://localhost:3000/ にアクセス ? git rm public/index.html
  • 12. ? r g scaffold Competition name:string ? config/routes.rb ? test/fixtures/competitions.yml (RSpec の場合は spec/fixtures/competions.yml) % cat test/fixtures/competitions one: name: 招き猫オープン two: name: 閑古鳥杯 ? rake db:migrate
  • 13. ? r g scaffold Event competition_id:integer name:string is_male:boolean % cat test/fixtures/events one: competition_id: 1 name: MyString is_male: false two: ... ? 「大会」と関連付けるため、上記を修正 ? boolean 型の注意点 t.boolean :is_male, :null => false, :default => true
  • 14. % cat test/fixtures/events % cat app/model/event.rb one: class Event < ActiveRecord::Base competition: one belongs_to :competition name: 18歳以下男子シングルス end is_male: true two: competition: one name: 18歳以下女子シングルス is_male: false three: ... ? rake db:fixtures:load でテストデータを読み込み
  • 15. ? テストデータが多すぎるのはNG ? 例えば users.yml は 3人~5人 ぐらいじゃね? ? テストプログラムでパターンが足りない場合は動 的にテストデータ生成
  • 16. ? rc ? Competition.all ? Event.all ? Event.first ? Event.all[0].competition ? Event.all[0].competition.event # ←エラー % cat app/model/competition.rb class Event < ActiveRecord::Base has_many :events end ? competition has many events ? Event.all[0].competition.event # ←Good! ? Competition.find_by_name(‘閑古鳥杯’).events.empty?
  • 17. ? そもそも has_many や belong_to の実装前 にテストから書く(ハズ) ? Event#competition_id は必須? ? 大会名は必須?名前の重複は変だよね? ? 種目名が1つの大会で重複もしかり ? 最大文字数とかは? ? ...ということで最初は validation のOK/NGの テストとかになるはず...
  • 18. ? r g scaffold User name:string is_male:boolean ? db/migrate/*users_rb の boolean はnull, default を忘れ ずに ? fixutres は男性3人、女性1人ぐらいで ? 認証はプラグインで(devise, Sorcery, ...) ? 権限も今回は無し
  • 19. ? r g model Entry event_id:integer user_id:integer ? test/fixtures/entries.yml % cat app/model/entry.rb class Entry < ActiveRecord::Base belongs_to :event belongs_to :user % cat app/model/user.rb end class User < ActiveRecord::Base has_many :entries has_many :events, :through => :entries % cat app/model/event.rb end class Event < ActiveRecord::Base has_many :entries has_many :users, :through => :entries end
  • 20. competitions id name users id events name id is_male competition_id (FK) name entries is_male id event_id (FK) user_id (FK)
  • 21. ? ログイン ? 大会一覧(種目一覧) ? 管理者 ? 利用者 ? 大会と種目の編集 ? 申込み一覧(自分の ? 会員一覧 み) ? 申込み一覧 ? 申込み 何から手をつけようか??? 大会は scaffold のままでほとんどOK。
  • 22. ? rake routes | grep events で基本を把握する ? competition has_many events を routes.rb で表現 ? events を competition にぶら下げる ? そして、再度 rake routes | grep events * config/routes.rb * resources :users resources :competitions do resources :events, :except => [:index] end ? 種目の一覧って2通り考えられる? ? 1つの大会に着目したときの、それにぶら下がる一覧 ? とにかく全て(大会の域を超えて)の一覧 ←本当に必要か?
  • 23. ? ここでは実際のソースを見ながら解説します ? 以下、一部抜粋 class EventsController < ApplicationController before_filter :find_competition def new @event = @competition.events.new end
  • 24. ? competitions#show に種目の CRUD を挿入 <p> <b>Name:</b> <%= @competition.name %> </p> <table border="1"> <tr> <th>Name</th> <th>Is male</th> <th></th> <th></th> <th></th> </tr> <%= render @competition.events %> </table> <%= link_to 'New Event', new_competition_event_path(@competition) %>
  • 25. ? 将来、権限機能を実装するのは明らかだ ? 基本、管理者は何でもできる ? ということは全てのUserの申込も操作できねば ? 申込の操作が実装できれば、後は「一般Userの申 込も流用できそうだ」 ? …ということで(私は)申込を users#show に実装することに決めた
  • 26. <table border="1"> <tr> <th>Competition</th> <th>Event</th> <th></th> </tr> <% @user.entries.each do |entry| %> <tr> <td><%= entry.event.competition.name %></td> <td><%= entry.event.name %></td> <td><%= link_to 'Cancel', entry, :confirm => 'Are you sure?', :method => :delete %></td> </tr> <% end %> </table> <h2>申込</h2> <%= form_tag(entries_path, :method => :get) do %> <%= ("competition", "id", Competition.all.collect {|| [ .name, .id ] }) %> <%= hidden_field_tag :user_id, @user.id %> <%= submit_tag '次へ' %> <% end %>
  • 27. competitions id name users id events name id is_male competition_id (FK) name entries is_male id event_id (FK) user_id (FK)
  • 28. ? entry.event.competition... が冗長だ ? entry から competition は1つなのに... ? API 見ると delegate メソッド発見! class Entry < ActiveRecord::Base delegate :competition, :to => :event ... ? entry.competition って書けた。Happy! ? この辺りは経験とか勉強の度合いで感じ取る ? def competition; event.competition; end
  • 30. ? ActiveRecord::Associations の has_many なモデルのCRUD画面作成 ? 「多対多」の関係(has_many :through) ? routing, controller, view ? 隙を見て rails.vim の話やその他もろもろ... ? 仕様を満たしていくまでの考え方、開発リ ズム ? つっこみ歓迎w
  • 31. Agile Web Development with Rails (4th edition) http://pragprog.com/book/rails4/agile-web-development-with-rails RailsによるアジャイルWebアプリケーション開発 第3版(注意!Rails2 です) http://ssl.ohmsha.co.jp/cgi-bin/menu.cgi?ISBN=978-4-274-06785-3
  • 32. ? たくさんありすぎ… ? @IT自分戦略研究所 http://jibun.atmarkit.co.jp/scenter/ittrain/ (1日1回、RSSフィードで地味にやってます) ? ITトレメ Ruby技術者認定【Gold】試験 ? ITトレメ Rails 3 技術者認定ブロンズ試験