ActiveRecord sample (Ruby)

Ruby code posted by Teggy
created at 17 Apr 17:04

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require 'rubygems'
require 'activerecord'

require 'pp'

ActiveRecord::Base.establish_connection :adapter           => 'postgresql', 
                                        :database          => 'ferry',
                                        :username          => 'grust',
                                        :password          => '...',
                                        :port              => 5432,
                                        :host              => 'localhost',
                                        :allow_concurrency => true
ActiveRecord::Base.logger = Logger.new(STDOUT)                                      

class Department < ActiveRecord::Base
  has_many :employees, :foreign_key => :dept     
end

class Employee < ActiveRecord::Base
  belongs_to :department, :foreign_key => :dept
end

e = []
Department.find(:all).each { |d| pp d.employees }

Department.find(:all, :include => :employees).each { |d| pp d.employees } 
980 Bytes in 3 ms with coderay