Chris Muench
Chris Muench

Reputation: 18318

Ruby On Rails One-One relationship On Same Table

In my rails application I have a table with the following columns (table name categories)

How do I setup the relationship so I can do something like @category.parent.title?

Upvotes: 1

Views: 630

Answers (2)

rmk
rmk

Reputation: 4455

Use the acts_as_tree gem?

More info here.

Upvotes: 0

user142019
user142019

Reputation:

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.

Upvotes: 5

Related Questions