How would you map the dashed association using ActiveRecord? Is there any ActiveRecord-way of doing it? What I want is to get all unique available promotions for a particular item.
Here are things I thought but I don't really like:
Using custom finder
class Item < ActiveRecord::Base
has_many :promotions, :finder_sql => "SELECT..FROM promotions..purchase_option_id IN (...)"
end
The dirty and inefficient but rubyish way
my_item.purchase_options.map(&:promotion).uniq
Breaking the PurchaseOption
<->Promotion
HABTM association and creating a new one for Item
<->PromotedPurchaseOption
class Item < ActiveRecord::Base
has_many :promoted_purchase_options
has_many :promotions, :through => :promoted_purchase_options
end
Other thoughts
I don't think it's a good idea to use named scopes or defining new methods This won't simply work or will execute inefficient queries if using them:
Item.first.promotions.count
Item.first.promotions.available
Promotion.first.items.all