Nie można dołączyć tabel łączących się w Railsach
Mam 2 modele
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
W szczególności kategoria ma element nadrzędny zatytułowany „herbata”, a ten przedmiot ma wiele przedmiotów dla dzieci: „czarna herbata”, „biała herbata” ...
Muszę wybrać produkty, które należą dorodzic kategoria „herbata”. Oto jak to robię:
Product.joins(:category=>:parent).where(:category=>{:parent=>{:id=>1}}).all
Zgłasza wyjątek (nie można go dobrze sformatować)
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
z powodu nieznanej kolumny parent.id.
Oczywiście zapytanie powinno być (działa idealnie):
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
Próbowałem nawet
Product.joins(:category=>:parent).where(:category.parent=>{:id=>1}).all
i to nie pomogło
Proszę, twoje myśli.