awesome_nested_setを導入したら最初にrebuild!しよう
2012-12-21
ツリー構造を表現するGemとして、 awesomenestedset があります。
has_many
ではなく、親子関係を持たせたい時に使っています。
簡単な使い方
# Create a root node:
science = Category.create!(name: 'Science')
# Put a new thing inside this root node:
physics = Category.create!(name: 'Physics')
physics.move_to_child_of(science)
# Put another thing inside the "physics" node:
gravity = Category.create!(name: 'Gravity')
gravity.move_to_child_of(physics)
# Reload the root node:
science.reload
# Now you should have something that resembles this:
# science
# '-- physics
# '-- gravity
詳しくは チートシートを見てください。
ハマりどころ
導入したら必ず rebuild!
しましょう。
Category.rebuild!
既存のモデルにこの機能を追加した際、rebuild!
を忘れていて、随分と無駄な時間を過ごしました。
rebuild!
しないとcomparison of Fixnum with nil failed
などと言われ、困惑すること請け合いです。