Rails


            2007.11.26


        y.ohba@everyleaf.com
...   C++   Java(7 )     Ruby(1.5     )

Perl CGI, EJB, Servlet/JSP (Struts)
orz
Ruby on Rails



 Java



        Ruby on Rails
Ruby on Rails   (^0^)/
Award on Rails 2006
•   Web

•
•

          http://www.kozuchi.net
Ruby on Rails   100%

EC



SNS
Ruby on Rails




  http://www.everyleaf.com
Award on rails 2007
                   BookScope

http://bookscope.net/
BookScope

Amazon WebService




   .org
Ruby on Rails

                /
Ruby on Rails



Ruby on Rails
Hello, RoR



Scaffold
              CRUD

Session, Request
Rails




Ruby

           etc...
API

             Rails   , Plugin

Ruby
Hash   Array
 REST


         ActiveRecord
                            DRY
Module    Mix-in
                   Ajax
      ...
- IDE, SVN, Trac, Wiki, UML
Ruby
    Java



0

||=



Hash       Array
Ruby - 0
s=0
if s
  p quot;0           quot;
end


           nil       false

      0
nil
if value != nil
  ...


if value
  ...


unless value.nil?
 ...
1
if !id
  raise quot;no idquot;
end
Java

raise quot;no idquot; if !id


raise quot;no idquot; unless id
             unless
Hash   Array




API
Hash
hash = {key => value, key => value, ...}

value = hash[key]


obj.foo( {key => value, key => value} )
obj.foo(key => ...
Array

     admin= nil
     for element in @array
       if element.name == quot;adminquot;
         admin = element
     ...
public, protected, private   Java

                   Module
||=
name ||= quot;       quot;




name || name = quot;           quot;
ex) value += 1
private, protected, public


            Ruby
 private
            Java

            Ruby
protected
            Java

    ...
private
item.my_private_method
self.my_private_method
          private



my_private_method


          private
Ruby
RoR
                - public

            - private

protected

  ActiveRecord
class Foo
  def self.foo
    p ‘foo’
  end
end

                 > Foo.foo
                 > f = Foo.new
                ...
ActiveRecord
:joins   :include
Acts as List
ActiveRecord
O/R

  RDB

ActiveRecord

  “              ”
ActiveRecord


table              class

users              User



                    user
          (id = 1, name = ‘   ’)
class


table

        class           class
(1)
  Item, Book, Dvd
        items

                           Item


items

                    Book          Dvd
Item.find(:all, :order => ‘created_at desc’)
              DVD,

Book.find(:all, :order => ‘created_at desc’)
type
type

 items
                                    Item


type: varchar(30)   quot;Bookquot;

                             B...
(2)

        Manager Developer
                                 ProjectMember
project_members               class

     ta...
item_type   role
                   if
Polymorphic Association
(Association)

                       =
          (=     )

belongs_to

has_one
has_many
belongs_to

            Employee             Office     offices
employees


office_id
             belongs_to :office
class Em...
Controller    Employee
@employee = Employee.find(params[:id])

             View
<table>
  <tr>
    <th>      </th>
    <td...
has_one
                                   has_one :head

              Employee             Office      offices
employees

...
has_many
                               has_many :employees

              Employee             Office       offices
employe...
has_many
 Controller   office
@office = Office.find(params[:id])


              View
<% for e in @office.employees -%>
 <div><...
Office   offices


            Employee
employees


                                 Factory     factories



    Employee  ...
...
class Employee < ActiveRecord::Base
  belongs_to :office
  belongs_to :factory
end

<% if @employee.office %>
 <%= @empl...
class Employee < ActiveRecord::Base
  belongs_to :workplace
end
     Office     Factory    WorkpPlace


<%= @employee.workp...
has_many :employees
                                has_one :head


            Employee             Office     offices
empl...
has_many :employees, :as => work_place

                                      Office     offices
 work_place_id
 work_place_...
(xxx_type   )
class Employee < ActiveRecord::Base
  belongs_to :work_place, :polymorphic => true
end

class Office < ActiveRecord::Base
 ...
work_place_id,
        work_place_type


xxx_id, xxx_type     xxx

      xxx_type             SQL
conditions
      ActiveR...
Comment
    comments


 commentable_id
commentable_type
class Item < ActiveRecord::Base
    has_many :comments, :as => :commentable
  end



@item.comments.create(:body => ‘     ...
(xxx_)type



             Up
ActiveRecord



               Save
#
def after_update
 children.each {|c| c.save!}
end
before_validation



    after_destroy
-
     Controller


       Model



Rails API
:joins            :include


employees = Employee.find(:all,
    :select => quot;employee.*quot;,
    :conditions => [quot;...
:joins        :include (2)
                                  DB



class Employee < ActiveRecord::Base
  belongs_to :office...
API

           validates_presence_of etc...



              = errors

def validate
 errors.add_to_base(“         ”) unle...
if @employee.save
    flash[:notice] = ‘                ’
    redirect_to :action => ‘index’
  else
    render :action => ‘...
API

API




  ActiveHeart Plugin

  GetText , Scpecial Generator
Acts as List

has_many
Acts as List
                position
class Note < ActiveRecord::Base
  has_many :lines, :order => quot;positionquot;
end
...
has_many




 )
class Employee < ActiveRecord::Base
  belongs_to :office, :counter_cache => true
end

   offices              employees_coun...
ActiveRecord

API

      Association   (has_many   )




 http://api.rubyonrails.org/
REST

Controller          URL




entries/new_comment/1
entries/1/comments/new
         entries/1/comments/3
REST

            Controller



routes.rb

Filter

            @book
rails 2.0



 REST
acts_as_authenticated plugin

 before_filter



before_filter :load_user
private
def load_user
  @user = User.find(session[:u...
@entries = Entry.secure_find(@user, options)
create              save

update




validates_association

:dependent => :destroy
acts_as_paranoid
file_column

RMagick




I/F
GetText (gem
      )

Globalize
acts_as_taggable


SpecialGenerator
        Scaffold
Rails
Ruby DSL
          DSL =

has_many :employees

  AR



  Rails

  = DSL
String



  under_score, pluralize ...
acts_as_paranoid
Module   Mix-in
Java



 Ruby

class Object
  def yeah
    p ‘yeah!’
  end
end
Module                    Mix-in
  Module



module Human
 def age(date)
   #                ()
 end
end
                 ...
ActiveRecord::Base


                                Module
 Office            Factory

include               include
     ...
include



module WorkPlace
 #
 def self.included(base)
   base.has_many :employees,
                   :as => :work_place...
Module
module WorkPlace
 module ClassMethods
   def foo
    p ‘foo’
   end
 end
 def self.included(base)
   base.extend(Cl...
private



obj.__send__(:himitsu)

       Ruby1.9
clazz.__send__(:define_method, :yeah) {
  p ‘yeah!’
}
(^o^)/
init.rb



Module
include

  DSL

  ) acts_as_paranoid
NULL



email

def email=(value)
 value = nil if value.blank?
 self[:email] = value
end
class User < ActiveRecord::Base
  nullify_blank :email, :address, :tel, :fax, :url
end
vendor/plugins/my_extension/init.rb

class ActiveRecord::Base
  def self.nullify_blank(*args)
    args.each do |attr|
    ...
Module   Helper


DSL
Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子
Upcoming SlideShare
Loading in …5
×

Ruby on Rails ステップアップ講座 - 大場寧子

10,759 views
10,560 views

Published on

11/26 (株)ウェブキャリア セミナー資料

DLはこちらからどうぞ
http://ko.meadowy.net/~nay/ruby/2007/seminar_ohba_2007_11_26-1.pdf

Published in: Technology
3 Comments
12 Likes
Statistics
Notes
  • 使わないほうが良いということは別にないと思います。しかし、もちろん自動で結合されるもので足りていれば特に使う必要がないでしょう。経験上は、:joinsで細かい制御をする必要がある(そのほうが効率がよい)ケースもあると考えています。
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • コメントが何故か文字化けしてしまいました。どうしたら良いかわかりません。すみません。
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • プレゼン大変勉強になります。'RailsによるアジャイルWebアプリケーション 2nd ediion' (以下Rails本)を読みながらRailsを勉強しているのですが、このプレゼンを見ながら復習にふけっています。本当にありがとうございます。

    一点、気がついた点が有ったのでコメントさせてください。
    :joins (プレゼン P.69)についてなんですが、Rails本P.278に寄ると、「詳しくは18章で説明しますが、おそらくfindで:joinsパラメータを使うことはめったにありません。ほとんどの場合、一般的なテーブル間の結合は Active Record で自動的に処理されます。」とありました。

    :joins は、データベース依存の構文をコード内に埋め込む事になるので、使わない方が良いのかなと思いました。
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
10,759
On SlideShare
0
From Embeds
0
Number of Embeds
424
Actions
Shares
0
Downloads
181
Comments
3
Likes
12
Embeds 0
No embeds

No notes for slide

Ruby on Rails ステップアップ講座 - 大場寧子

  1. 1. Rails 2007.11.26 y.ohba@everyleaf.com
  2. 2. ... C++ Java(7 ) Ruby(1.5 ) Perl CGI, EJB, Servlet/JSP (Struts)
  3. 3. orz
  4. 4. Ruby on Rails Java Ruby on Rails
  5. 5. Ruby on Rails (^0^)/
  6. 6. Award on Rails 2006 • Web • • http://www.kozuchi.net
  7. 7. Ruby on Rails 100% EC SNS
  8. 8. Ruby on Rails http://www.everyleaf.com
  9. 9. Award on rails 2007 BookScope http://bookscope.net/
  10. 10. BookScope Amazon WebService .org
  11. 11. Ruby on Rails /
  12. 12. Ruby on Rails Ruby on Rails
  13. 13. Hello, RoR Scaffold CRUD Session, Request
  14. 14. Rails Ruby etc...
  15. 15. API Rails , Plugin Ruby
  16. 16. Hash Array REST ActiveRecord DRY Module Mix-in Ajax Plugin
  17. 17. - IDE, SVN, Trac, Wiki, UML
  18. 18. Ruby Java 0 ||= Hash Array
  19. 19. Ruby - 0 s=0 if s p quot;0 quot; end nil false 0
  20. 20. nil if value != nil ... if value ... unless value.nil? ...
  21. 21. 1 if !id raise quot;no idquot; end Java raise quot;no idquot; if !id raise quot;no idquot; unless id unless
  22. 22. Hash Array API
  23. 23. Hash hash = {key => value, key => value, ...} value = hash[key] obj.foo( {key => value, key => value} ) obj.foo(key => value, key => value) {}
  24. 24. Array admin= nil for element in @array if element.name == quot;adminquot; admin = element break end end admin = @array.detect{|e| e.name == quot;adminquot; }
  25. 25. public, protected, private Java Module
  26. 26. ||= name ||= quot; quot; name || name = quot; quot; ex) value += 1
  27. 27. private, protected, public Ruby private Java Ruby protected Java Java Ruby public Java
  28. 28. private item.my_private_method self.my_private_method private my_private_method private
  29. 29. Ruby
  30. 30. RoR - public - private protected ActiveRecord
  31. 31. class Foo def self.foo p ‘foo’ end end > Foo.foo > f = Foo.new > f.class.foo
  32. 32. ActiveRecord
  33. 33. :joins :include
  34. 34. Acts as List
  35. 35. ActiveRecord O/R RDB ActiveRecord “ ”
  36. 36. ActiveRecord table class users User user (id = 1, name = ‘ ’)
  37. 37. class table class class
  38. 38. (1) Item, Book, Dvd items Item items Book Dvd
  39. 39. Item.find(:all, :order => ‘created_at desc’) DVD, Book.find(:all, :order => ‘created_at desc’)
  40. 40. type
  41. 41. type items Item type: varchar(30) quot;Bookquot; Book Dvd type
  42. 42. (2) Manager Developer ProjectMember project_members class table class class Developer Manager
  43. 43. item_type role if
  44. 44. Polymorphic Association
  45. 45. (Association) = (= ) belongs_to has_one has_many
  46. 46. belongs_to Employee Office offices employees office_id belongs_to :office class Employee < ActiveRecord::Base belongs_to :office end belongs_to( )
  47. 47. Controller Employee @employee = Employee.find(params[:id]) View <table> <tr> <th> </th> <td><%= @employee.office.name %></td> </tr> </table>
  48. 48. has_one has_one :head Employee Office offices employees office_id class Office < ActiveRecord::Base has_one :head, :class_name => ‘Employee’, :conditions => “head = 1” end has_xxx ( )
  49. 49. has_many has_many :employees Employee Office offices employees office_id class Office < ActiveRecord::Base has_many :employees end has_one has_many
  50. 50. has_many Controller office @office = Office.find(params[:id]) View <% for e in @office.employees -%> <div><%= e.name %></div> <% end ->
  51. 51. Office offices Employee employees Factory factories Employee Office Factory
  52. 52. ... class Employee < ActiveRecord::Base belongs_to :office belongs_to :factory end <% if @employee.office %> <%= @employee.office.name %> <% else -%> <%= @employee.factory.name %> <% end -%>
  53. 53. class Employee < ActiveRecord::Base belongs_to :workplace end Office Factory WorkpPlace <%= @employee.workplace.name %>
  54. 54. has_many :employees has_one :head Employee Office offices employees office_id belongs_to :office
  55. 55. has_many :employees, :as => work_place Office offices work_place_id work_place_type Work Employee employees Place Factory factories belongs_to :work_place, :polymorphic => true
  56. 56. (xxx_type )
  57. 57. class Employee < ActiveRecord::Base belongs_to :work_place, :polymorphic => true end class Office < ActiveRecord::Base has_many :employees, :as => :work_place end class Factory < ActiveRecord::Base has_many :employees, :as => :work_place end
  58. 58. work_place_id, work_place_type xxx_id, xxx_type xxx xxx_type SQL conditions ActiveRecord
  59. 59. Comment comments commentable_id commentable_type
  60. 60. class Item < ActiveRecord::Base has_many :comments, :as => :commentable end @item.comments.create(:body => ‘ ’)
  61. 61. (xxx_)type Up
  62. 62. ActiveRecord Save
  63. 63. # def after_update children.each {|c| c.save!} end
  64. 64. before_validation after_destroy
  65. 65. - Controller Model Rails API
  66. 66. :joins :include employees = Employee.find(:all, :select => quot;employee.*quot;, :conditions => [quot;offices.prefectue_id = ?quot;, prefecture_id], :joins => quot;left join offices on employees.office_id = offices.idquot;) :joins SQL join
  67. 67. :joins :include (2) DB class Employee < ActiveRecord::Base belongs_to :office end employee = Employee.find(@id, :include => :office)
  68. 68. API validates_presence_of etc... = errors def validate errors.add_to_base(“ ”) unless quantity % 2 == 0 end
  69. 69. if @employee.save flash[:notice] = ‘ ’ redirect_to :action => ‘index’ else render :action => ‘new’ end Controller save <%= error_messages_for ‘employee’ %> View
  70. 70. API API ActiveHeart Plugin GetText , Scpecial Generator
  71. 71. Acts as List has_many
  72. 72. Acts as List position class Note < ActiveRecord::Base has_many :lines, :order => quot;positionquot; end class Line < ActiveRecord::Base rails 2.0 belongs_to :note acts_as_list :scope => :note end position
  73. 73. has_many )
  74. 74. class Employee < ActiveRecord::Base belongs_to :office, :counter_cache => true end offices employees_count 0
  75. 75. ActiveRecord API Association (has_many ) http://api.rubyonrails.org/
  76. 76. REST Controller URL entries/new_comment/1 entries/1/comments/new entries/1/comments/3
  77. 77. REST Controller routes.rb Filter @book
  78. 78. rails 2.0 REST
  79. 79. acts_as_authenticated plugin before_filter before_filter :load_user private def load_user @user = User.find(session[:user_id]) end
  80. 80. @entries = Entry.secure_find(@user, options)
  81. 81. create save update validates_association :dependent => :destroy
  82. 82. acts_as_paranoid
  83. 83. file_column RMagick I/F
  84. 84. GetText (gem ) Globalize
  85. 85. acts_as_taggable SpecialGenerator Scaffold
  86. 86. Rails
  87. 87. Ruby DSL DSL = has_many :employees AR Rails = DSL
  88. 88. String under_score, pluralize ... acts_as_paranoid
  89. 89. Module Mix-in
  90. 90. Java Ruby class Object def yeah p ‘yeah!’ end end
  91. 91. Module Mix-in Module module Human def age(date) # () end end include class Employee < ActiveRecord::Base include Human end
  92. 92. ActiveRecord::Base Module Office Factory include include WorkPlace
  93. 93. include module WorkPlace # def self.included(base) base.has_many :employees, :as => :work_place end end
  94. 94. Module module WorkPlace module ClassMethods def foo p ‘foo’ end end def self.included(base) base.extend(ClassMethods) end end
  95. 95. private obj.__send__(:himitsu) Ruby1.9
  96. 96. clazz.__send__(:define_method, :yeah) { p ‘yeah!’ }
  97. 97. (^o^)/
  98. 98. init.rb Module include DSL ) acts_as_paranoid
  99. 99. NULL email def email=(value) value = nil if value.blank? self[:email] = value end
  100. 100. class User < ActiveRecord::Base nullify_blank :email, :address, :tel, :fax, :url end
  101. 101. vendor/plugins/my_extension/init.rb class ActiveRecord::Base def self.nullify_blank(*args) args.each do |attr| define_method(:quot;#{attr.to_s}=quot;) do |v| v = nil if v.blank? self[attr.to_sym] = v end end end end
  102. 102. Module Helper DSL

×