Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Parent/child relationships

I'm currently using a standard one-to-one relationship to handle parent/child relationships:

class Category < ActiveRecord::Base
  has_one :category
  belongs_to :category
end

Is there a recommended way to do it or is this ok?

like image 206
bcoughlan Avatar asked Feb 07 '11 01:02

bcoughlan


People also ask

What are the five parent/child relationships?

have certain qualities that remain constant. They are built on safety, unconditional love, mutual respect, acceptance and flexibility.

What is parent/child relationship theory?

A form of filial therapy, child-parent relationship theory (CPRT) teaches parents to use child-centered play therapy (CCPT) skills with their children. Based on attachment theory, CPRT espouses that a secure bond between parent and child is mandatory for children's healthy development.

What is the best parent/child relationship?

Trust and respect are essential to a positive parent-child relationship. In the early years with your baby, developing trust is important. Your baby will feel secure when they learn they can trust you and other main carers to meet their needs.


1 Answers

You will need to tweak the names you are using to get this working - you specify the name of the relationship, and then tell AR what the class is:

class Category < ActiveRecord::Base
  has_one :child, :class_name => "Category"
  belongs_to :parent, :class_name => "Category" 
end
like image 170
Toby Hede Avatar answered Oct 25 '22 08:10

Toby Hede