Prev | Current Page 107 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

If you have an invalid record for whatever reason, the
attributes will be updated anyway. This is important to know, because if you??™re ever in a situation
in which you have an invalid object but still need to toggle a flag or set a status, you can.
While creation of related objects will occur when your object is created, as described in
the ???Creating a Record??? section earlier, it is important to note that associated objects do not
behave similarly when updates are performed. For example, in the following code, the modification
to the Category model will not be saved:
p = Product.find 1
p.category.name = "Changed"
p.save
Therefore, you must explicitly call save on associated objects, for example:
p = Product.find 1
p.category.name = "Changed"
p.category.save
That being said, it is not necessarily commonplace to perform the type of operation illustrated
here, especially in a web-based application.
Deleting a Record
Sometimes, data has to go away. We know, it??™s not always a pleasant thought, but not all data is
useful all the time.


Pages:
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119