Prev | Current Page 49 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


The attributes are usually directly mapped from the fields of your database table. So for example,
if our Account table had an Account_Username field, then our Account Active Record objects
would have a corresponding Account_Username attribute. The following example shows one way
of directly setting an attribute??™s value as well as how to access the value of a given attribute:
example.Account_Username = "Falicon"
puts "Your username is now #{example.Account_Username}"
Saving the Attributes as a Record in the Database
It??™s important to remember that when you are working with an Active Record object you are
really only setting and accessing the attributes of a Ruby object. Your changes are not reflected
within your database until you make a call to the ActiveRecord::Base.save method.
The save method is where most of the real action and power of the Active Record library
takes place:
Example.save
It??™s this method that has built-in support for things like callbacks, data validations, and
many of the other features explained throughout the remainder of this book.


Pages:
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