vendredi 20 novembre 2015

how to get scope with self reference in Rails?

i have a model

class Category < ActiveRecord::Base
  belongs_to :parent, :class_name => 'Category'
  has_many :children, :class_name => 'Category', :foreign_key => 'parent_id'
end

I want to a Scope with select all Category in tables categories with category.children == nil or (category.children.count == 0).

Pls help me.

Aucun commentaire:

Enregistrer un commentaire