vendredi 31 juillet 2015

Recursive SQL using Rails 4 (modeling)

I read a tutorial, but I did not understand properly. I will quote it here and present the problem that seek to solve.

Tutorial

My problem: I'm doing a forum that can have various categories and subcategories. Then decide to use SQL Recursive PostgreSQL to facilitate me and not pollute my db with unnecessary tables. Suppose I have a table categories, and it has the categories_id field. Then the model would look something like this:

  attr_accessible :parent

  belongs_to :parent, :class_name => "Category"
  has_many :children, :class_name => "Category", :foreign_key => 'categories_id'

  scope :top_level, where(:categories_id => nil)

1- I did not understand fully if parent and children are a field in the table or not. 2- Why did he have to create a attr_accessible :parent? 3- Why the creation of scope?

Thanks,

Aucun commentaire:

Enregistrer un commentaire