Prev | Current Page 82 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


Additionally, you should only use the id attribute to set the primary key. To retrieve the value
of the primary key, you must use your overridden attribute name.
For example, if we??™ve overridden the account primary key to be account_number, and we
want to use a custom key format, our Account creation code would need to be as follows:
account = Account.new
account.id = "X5476"
account.save
And to retrieve the account_number of an account, you would use this:
puts account.account_number #=> X5476
Retrieving Objects from the Database
With the groundwork laid regarding Active Record knowledge about our database, the dynamic
nature of Ruby Active Record is able to help us work with our objects. For instance, to retrieve
objects from the database we have a core method find. If we know the value of the primary
key that we want, for instance 1, we can simply call it:
Account.find(1)
In addition, it is possible to use a feature of Active Record called dynamic finders. These
allow you to easily find records by their attribute values.


Pages:
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94