开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜