public
Description: my random ruby scripts
Homepage:
Clone URL: git://github.com/kastner/ruby-junk.git
Click here to lend your support to: ruby-junk and make a donation at www.pledgie.com !
ruby-junk / ar_playground_extra_methods.rb
100755 62 lines (50 sloc) 1.561 kb
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env ruby
%w|rubygems active_record irb|.each {|lib| require lib}
 
class Fruit < ActiveRecord::Base
validates_uniqueness_of :name
belongs_to :stand
named_scope :red_fruits, :conditions => "color = 'red'"
named_scope :recent, lambda { { :conditions => ['created_at > ?', 10.seconds.ago] }}
def self.by_color(color)
with_scope :conditions => {:color => color} do
find(:all)
end
end
def self.red
find(:all, :conditions => "color = 'red'")
end
end
 
class Stand < ActiveRecord::Base
has_many :fruits do
def newest(color = nil)
if color
find(:all, :conditions => ["created_at > ? AND color = ?", 10.seconds.ago, color])
else
find(:all, :conditions => ["created_at > ?", 10.seconds.ago])
end
end
end
end
 
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => ":memory:"
)
 
ActiveRecord::Schema.define do
create_table :stands do |t|
t.string :name
t.string :address
end
create_table :fruits do |t|
    t.string :name
    t.string :color
    t.belongs_to :stand
    t.timestamps
  end
end
 
stand = Stand.create(:name => "Joe's Fruit Market", :address => "Around the corner")
Fruit.create(:stand => stand, :name => "apple", :color => "red")
Fruit.create(:stand => stand, :name => "pear", :color => "brown")
Fruit.create(:stand => stand, :name => "red grape", :color => "red")
Fruit.create(:stand => stand, :name => "orange", :color => "orange")
 
IRB.start if __FILE__ == $0