Yogzzz
Yogzzz

Reputation: 2795

Trying to return AREL not Array in Rails

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

Answers (1)

Michelle Tilley
Michelle Tilley

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

Related Questions