Unable to join self-joins tables in Rails

前端 未结 1 854
臣服心动
臣服心动 2020-12-22 04:53

I have 2 models

class Category < ActiveRecord::Base
  belongs_to :parent, :class_name => \"Category\"
  has_many :children,  :class_name => \"Catego         


        
相关标签:
1条回答
  • 2020-12-22 05:45

    While the operation of joins() here is pretty smart, the where() part isn't so clever. AFAIK it doesn't know anything about the joins and really just converts its arguments to strings. As such, try this:

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

    In order to avoid bleeding the name used internally by AR to your code, consider using AREL for your query. There have been some excellent railscasts on that subject recently.

    0 讨论(0)
提交回复
热议问题