94: def demo
95:
96: Person.transaction do
97: Rating.db_execute( "DELETE FROM #{Rating.table}" )
98: MediumEntry.db_execute("DELETE FROM #{MediumEntry.table}")
99: Movie.db_execute( "DELETE FROM #{Movie.table}" )
100: Medium.db_execute( "DELETE FROM #{Medium.table}" )
101: Person.db_execute( "DELETE FROM #{Person.table}" )
102: end
103: anja = Person.new(:name => 'Anja' ).save
104: phillip = Person.new(:name => 'Phillip').save
105: wilson = Person.new(:name => 'Wilson' ).save
106: american_beauty = Movie.new(:name => 'American Beauty').save
107: naruto = Movie.new(:name => 'Naruto').save
108: koyaanisqatsi = Movie.new(:name => 'Koyaanisqatsi').save
109: medium_a = nil
110: Medium.transaction do
111: medium_a = Medium.new(:number => :auto).save
112: FlexiRecordDemo::MediumEntry.new(:medium => medium_a, :position => :last, :movie => naruto).save
113: end
114: medium_b = nil
115: Medium.transaction do
116: medium_b = Medium.new(:number => '42', :lent_to => phillip).save
117: MediumEntry.new(:medium => medium_b, :position => :last, :movie => koyaanisqatsi).save
118: MediumEntry.new(:medium => medium_b, :position => :last, :movie => american_beauty).save
119: end
120: Rating.new(:person => anja, :movie => american_beauty, :rating => Rational(7, 10)).save
121: Rating.new(:person => anja, :movie => koyaanisqatsi, :rating => Rational(9,10)).save
122: Rating.new(:person => phillip, :movie => koyaanisqatsi, :rating => Rational(6,10)).save
123: Rating.new(:person => phillip, :movie => koyaanisqatsi, :rating => Rational(8,10)).save
124: Rating.new(:person => wilson, :movie => naruto, :comment => 'Rasengan!').save
125:
126: person = Person.select1('WHERE "name" ILIKE $ ORDER BY "name" DESC LIMIT 1', 'P%')
127: puts "First person whose name is starting with 'P' is: #{person.name}."
128: puts "The following media are borrowed by him/her:"
129: person.borrowed_media.each do |medium|
130: puts "- ##{medium.number}"
131: medium.entries.each do |entry|
132: puts " - #{entry.movie.name}"
133: end
134: end
135: puts "He rated the following movies:"
136: person.rated_movies.each do |movie|
137: rating = movie.rel
138: puts "- #{movie.name}"
139: puts " - Rating: #{rating.rating ? rating.rating.to_s : 'none'}"
140: puts " - Comment: #{rating.comment || 'none'}"
141: end
142: anjas = Person.select('WHERE "name" = $', 'Anja')
143: if anjas.length == 1
144: anja = anjas.first
145: anjas_favourite = anja.rated_movies('WHERE "rel"."rating" NOTNULL ORDER BY "rel"."rating" DESC LIMIT 1').first
146: puts "The movie, which Anja likes most is: #{anjas_favourite ? anjas_favourite.name : 'N/A'}."
147: else
148: puts "There is more than one person named Anja."
149: end
150: nil
151: end