kastner (owner)

Revisions

gist: 141650 Download_button fork
public
Public Clone URL: git://gist.github.com/141650.git
Embed All Files: show embed
ar_playground_through.rb #
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
#!/usr/bin/env ruby
%w|rubygems active_record irb|.each {|lib| require lib}
 
class Guide < ActiveRecord::Base
has_many :guide_listings, :conditions => "guide_listings.active = 1"
has_many :listings, :through => :guide_listings
end
 
class GuideListing < ActiveRecord::Base
belongs_to :guide
belongs_to :listing
end
 
class Listing < ActiveRecord::Base
has_many :guide_listings
has_many :guides, :through => :guide_listings
named_scope :active, :conditions => "listings.active = 1"
end
 
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => ":memory:"
)
 
ActiveRecord::Schema.define do
create_table :guides do |t|
t.string :name
end
create_table :guide_listings do |t|
t.integer :guide_id
t.integer :listing_id
t.integer :active, :default => 0
end
create_table :listings do |t|
    t.string :name
    t.integer :active, :default => 0
  end
end
 
l1 = Listing.create(:name => "Shirt", :active => 1)
l2 = Listing.create(:name => "Pants", :active => 0)
l3= Listing.create(:name => "Skirt", :active => 1)
 
g = Guide.create(:name => "Clothes")
 
g.guide_listings << GuideListing.create(:guide => g, :listing => l1, :active => 1)
g.guide_listings << GuideListing.create(:guide => g, :listing => l2, :active => 1)
g.guide_listings << GuideListing.create(:guide => g, :listing => l3, :active => 0)
 
IRB.start if __FILE__ == $0