Selbstverknüpfung von Tabellen in Rails nicht möglich

Ich habe 2 Modelle

class Category < ActiveRecord::Base
  belongs_to :parent, :class_name => "Category"
  has_many :children,  :class_name => "Category", :foreign_key => "parent_id"
  has_many :products
  attr_accessible :description, :title, :parent

end

class Product < ActiveRecord::Base
  belongs_to :category
end

Insbesondere hat Category einen übergeordneten Artikel mit dem Titel "tea" und dieser Artikel hat viele untergeordnete Artikel: "black tea", "white tea" ...

Ich muss Produkte auswählen, die zu a gehörenElternteil Kategorie "Tee". So mache ich das:

Product.joins(:category=>:parent).where(:category=>{:parent=>{:id=>1}}).all

Es wird eine Ausnahme ausgelöst (kann nicht gut formatiert werden)

Product Load (0.8ms)  SELECT `products`.* FROM `products` INNER JOIN `categories` ON `categories`.`id` = `products`.`category_id` INNER JOIN `categories` `parents_categories` ON `parents_categories`.`id` = `categories`.`parent_id` WHERE `parent`.`id` = 1

ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'parent.id' in 'where clause': SELECT `products`.* FROM `products` INNER JOIN `categories` ON `categories`.`id` = `products`.`category_id` INNER JOIN `categories` `parents_categories` ON `parents_categories`.`id` = `categories`.`parent_id` WHERE `parent`.`id` = 1

wegen unbekannter parent.id Spalte.

Offensichtlich sollte die Abfrage sein (es funktioniert perfekt):

    SELECT `products`.* FROM `products` 
    INNER JOIN `categories` ON `categories`.`id` = `products`.`category_id` 
INNER JOIN `categories` as `parents_categories` ON `parents_categories`.`id` = `categories`.`parent_id` WHERE `parents_categories`.`id` = 1

Ich habe es sogar versucht

Product.joins(:category=>:parent).where(:category.parent=>{:id=>1}).all

und es hat nicht geholfen

Bitte, deine Gedanken.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage