Question

I have an Items table and an associated ItemPrices table setup with a has_many association. The ItemPrices table also has a tier_id field.

So the structure is basically

Item                      ItemPrices
---------                 ------------
id                        id
ancestry                  item_id
title                     tier_id
                          price

The Items table is setup as with has_ancestry and is working fine.

How can I grab a hierarchy of items where the itemprices are in a certain tier?

Était-ce utile?

La solution

it has to be somehow like this:

Item model:

 has_ancestry
 has_many :item_prices

 def self.tree_with_certain_tier(tier_id)
    scoped.includes(:item_prices).collect{|item| item.item_prices.with_certain_tier(tier_id)}.arrange
 end

ItemPrice model:

 belongs_to :item
 scope :with_certain_tier, lambda {|tier_id| where(:tier_id=>tier_id) }

and now you can grab what you need just with

Item.tree_with_certain_tier(pass_certain_tier_id_here)

i didn't check this but it has to be correct (or at least point you in right direction)

ask if something not clear or you will get any errors. sure we will fix them quickly

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top