Reputation: 2795
I have two models: Products and Tags through a products_tags join in a HABTM relationship.
I am currently defining my controller index as:
@stats = Product.all(:include => :tags).uniq
which returns an array. How can I return an Active Rel object? I tried added scoped, however received a no method error.
I need to find and list a unique list of tags, and be able to view what product each specif tag belongs to.
Upvotes: 1
Views: 649
Reputation: 159115
Try @stats = Product.includes(:tags).uniq
.
Note that uniq
turns the Relation into an Array; if you want to do something like SELECT DISTINCT
you'll want to use Product.includes(:tags).select('DISTINCT whatever')
.
Upvotes: 1