I have a class method Relationship.with_attributes
that I use to build up a complex scope based on multiple input parameters to return Relationship
objects associated with products that match certain attributes. In most cases, the information I match against is on the Product
model and all is well:
def self.with_attributes(attributes)
if (attributes.nil?)
scoped
else # (attributes.class == Hash)
conds = joins(:product)
attributes.each do |key, value|
case key
when "Category"
# FIXME This doesn't work yet
category = Category.find(value)
categories = [category] + category.descendants
categories.each { |c| conds.push(["categories.id = #{c.id}"], :or) }
else
conds.where(key => value)
end
end
end
conds
end
The challenge is I haven't been able to determine how to join in the Categorizations
model when my attribute is a category. Any advice is much appreciated. Abbreviated models are below.
class Relationship < ActiveRecord::Base
belongs_to :product
…
end
class Product < ActiveRecord::Base
has_many :relations
has_many :categorizations
…
end
class Categorizations < ActiveRecord::Base
belongs_to :product
…
end