Ruby On Rails One-One relationship On Same Table
In my rails application I have a table with the following columns (table name categories)
- id
- title
- summary
- parent_id
How do I setup the relationship so I can do something like @cat开发者_开发问答egory.parent.title?
belongs_to :category, :foreign_key => :parent_id
has_many :categories, :foreign_key => :parent_id
Note that if a category has no parent category and you try to access it anyway, you get an error.
Use the acts_as_tree gem?
More info here.
精彩评论