Prev | Current Page 238 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


By default, the acts_as_nested_set method assumes that the three foreign key field names
are parent_id for the reference to the parent record, lft for the reference to the lower sibling
boundary, and rgt for the upper sibling boundary. Of course, each of these defaults can be
overridden, but we??™ll save the details of that for the next step (since it??™s really done there).
Step 2: Defining Your acts_as_nested Method
Adding the acts_as_nested method is what really includes all the nested set functionality to
your model. There are four parameters that this method accepts: parent_column, left_column,
right_column, and scope. All of these parameters are optional, making the following example
the most basic working example possible:
class Comment < ActiveRecord::Base
acts_as_nested_set
end
CHAPTER 5 ?–  BONUS FEATURES 101
The optional parameters show very clearly how the nested set can work both like a list
and like a tree at the same time.
parent_column
Use the parent_column parameter to specify a name for the foreign key column in your table.


Pages:
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250