开发者

Ruby On Rails One-One relationship On Same Table

开发者 https://www.devze.com 2023-02-13 19:00 出处:网络
In my rails application I have a table with the following columns (table name categories) id title summary

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

精彩评论

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