Prev | Current Page 249 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


You can include references to any other objects or classes that you want to extend, inherit, or
otherwise manipulate, and you can write any methods for which you want this class to provide
functionality. However, it??™s important to remember that you must at least define the attributes
you specified in the mapping parameter and an initialize method, or your class really won??™t
know how to collect the database information into an object state.
Once again, an example helps explain the details a little better, so let??™s create a value object
for some basic song information. To make things a little more interesting than just grouping
things together, we??™ll also define a method that lets us automatically get all the songs of the
same name as the current record but recorded by other artists. Here??™s our new value object:
class Songinfo
attr_reader :artist, :songtitle, :songlength
def initialize(artist, songtitle, songlength)
@artist, @songtitle, @songlength = artist, songtitle, songlength
end
def by_other_artist
Song.


Pages:
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261