2. WHO
AM I ?
research & development director at pt jerbee indonesia
a newbie ruby programmer
a foss & gnu/linux enthusiast
a blogger
a comic artist (chickenstrip.org)
member of id-ruby community
4. buzzword, trend, hype
kok bisa? ada apa? cuma euforia? apa memang bagus?
ruby on rails that...
ruby on rails this...
ruby on rails that...
ruby on rails that...
ruby on rails this...
ruby on rails that...
5. code your idea ASAP!
technopreuneur, ubah ide anda menjadi web secepatnya !
6. powered by ruby on rails
top web berbasis rails berdasarkan alexa rating oktober 2008
7. powered by ruby on rails
non-publik? proyek? jobs? backend? intranet? outsorcing?
17. the rails way !
convention over configuration (COC)
ada struktur, penamaan dan aturan-aturan yang berlaku
don't repeat yourself (DRY)
menghemat waktu, mudah di-manage, reusable code
22. script/generate model Friendship user_id:integer
friend_id:integer relation:string
script/destroy model User
script/generate scaffold User name:string
address:string birthday:date
30. ActiveRecord
Object Relation Mapping (ORM)
Rails MVC's model foundation
Database Agnostic
Tables map to classes
Rows map to objects
Columns map to object attributes
Convention:
table = plural
model = singular with capital
31. User.find(1)
# SELECT * FROM users WHERE id=1
User.find_by_name 'Icha Aprillia'
# SELECT * FROM users WHERE name='Icha Aprillia'
User.find_by_login_and_email 'icha',
'icha92@yahoo.co.id'
# SELECT * FROM users WHERE login='icha' AND emai='icha92@yahoo.co.id'
User.find :all, :order => 'login DESC'
# SELECT * FROM users ORDER BY login DESC
User.find :all, :conditions => [quot;login=?quot;,quot;ichaquot;]
# SELECT * FROM users WHERE login='icha'
32. Model's Associations
Friend = User
User
id name address birthday
id name address birthday
Friendship
id user_id friend_id relation
33. MODEL'S ASSOCIATIONS
class User < ActiveRecord::Base
has_many :friendships
has_many :friends, :through => :friendships
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => quot;Userquot;,
:foreign_key => quot;friend_idquot;
end
User_1 had a friendship with his friend, the User_2.
User_1 could have many friendship with his friends.
34. User.find(1).friends
# SELECT * FROM users INNER JOIN friendships ON users.id =
friendships.friend_id WHERE ((friendships.user_id = 1)
# Listing User_1's friends
User.find(1).friends.count
# The numbers of User_1's friends
36. ActionController
Separation of business logic & presentation
No logic in view!
(Not so) Convention:
Controller name = plural
37. CONTROLLERS
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@friends = User.find(params[:id]).friends
end
...
Action “show” will be collecting user information and
all of user's friends information.