Ruby on Rails


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

Award on Rails 2006
orz
Award on Rails 2006
•   Web

•
•

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

EC



SNS
Ruby on Rails




 http://www.everyleaf.com
Ruby on Rails

                /
Ruby on Rails



Java
Hello, RoR



Scaffold
               CRUD

Session, Request
Rails




Ruby

           etc...
API

             Rails   , Plugin

Ruby
REST              Hash   Array

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

||=



Hash   Array
Ruby - 0
s=0
if s
  p "0           "
end

           nil       false

      0
nil
if value != nil
  ...


if value
  ...


unless value.nil?
 ...
1
if !id
  raise "no id"
end
Java

raise "no id" if !id


raise "no id" unless id
             unless
Hash   Array



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


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

     admin= nil
     for element in @array
       if element.name == "admin"
         admin = element
         break
       end
     end

admin = @array.detect{|e| e.name == "admin" }
public, protected, private   Java

                   Module
||=
name ||= "       "




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


            Ruby
 private
            Java

            Ruby
protected
            Java

            Java


 public
            Ruby            

            Java
Ruby



Java
item.save
item

self.save
self

save


            private
RoR

                - public

            - private

protected

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


> Foo.foo
> f = Foo.new
> f.class.foo
ActiveRecord
:joins   :include
Acts as List
class


table

        class       class
Item, Book, Dvd
         items


                             Item

items

                      Book          Dvd
type
type

 items
                                    Item

type: varchar(30)   "Book"

                             Book          Dvd

    type
Employee belongs_to :work_place
has_many :employees
                            has_one :employee


employees   Employee            Office    offices

office_id
            belongs_to :office
has_many :employees, :as => work_place

                                   Office     offices
work_place_id
work_place_type


employe                 Work
  es      Employee      Place     Factory    factories



          belongs_to :work_place, :polymorphic => true
work_place_id,
      work_place_type

xxx_id, xxx_type        xxx

id   type(          )

     xxx_type

has_many :through
(xxx_)type



→            Up
ActiveRecord



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


       Model



Rails API
:joins           :include


employees = Employee.find(:all,
    :select => "employee.*",
    :conditions => ["offices.prefectue_id = ?",
                      prefecture_id],
    :joins => "left join offices on
             employees.office_id = offices.id")

           :joins   SQL    join
:joins       :include (2)


class Employee < ActiveRecord::Base
  belongs_to :office
end


employee = Employee.find(@id,
           :include => :office)
Rails                             API

          validates_presence_of etc...



               = errors

def validate
 errors.add_to_base(“          ”) unless quantity % 2 == 0
end
API

:scope, :allow_nil




          ActiveHeart Plugin

          GetText , Scpecial Generator
Acts as List

has_many
Acts as List
             position



class Note < ActiveRecord::Base
  has_many :lines, :order => "position"
end


class Line < ActiveRecord::Base
  belongs_to :note                        rails 2.0
  acts_as_list :scope => :note
end
has_many




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

★offices              employees_count


★             0
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[:user_id])
end
@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
                  include

class Employee < ActiveRecord::Base
  include Human
end
/


   ActiveRecord::Base



 Office            Factory       Module

include               include
          WorkPlace
include

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



obj.__send__(:himitsu)

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


Module                     include



→ DSL

 ) acts_as_paranoid
Module   Helper



DSL
What
(^o^)/
Ruby on Rails
Award on rails 2007
                  BookScope

http://ko.meadowy.net/bookscope/
BookScope

Amazon WebService




   .org
10/19(   )        !!
http://rails.drecom.jp/vote



    BookScope

         m(_ _)m
Ruby on Rails 中級者を目指して - 大場寧子

Ruby on Rails 中級者を目指して - 大場寧子