Prev | Current Page 230 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

lower_item # => listdetails[1]
Acting as a Tree
Let??™s face it, lists are great, but they will only let you go so far in defining the relationship
among a collection??™s items. This is because lists lack depth. Say, for example, you want to build
a simple comment board application. You want your comment board to have various topics,
various threads within each topic, and various posts or replies within those threads. Actually,
lists could work for this application, but your code would get awfully messy, awfully fast. Luckily,
CHAPTER 5 ?–  BONUS FEATURES 97
there??™s a better structure to deal with this sort of problem??”trees. Even better, there??™s an Active
Record acts_as_tree method that was designed to help you to deal with collections of just this
sort!
Though it usually takes a little bit of work to visualize the concept of a tree structure, once
you do, it??™s quite simple to implement:
1. Define a foreign key column in your table that maps back to the primary key of the
same table.
2. Call the acts_as_tree method.


Pages:
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242