scientiffic
scientiffic

Reputation: 9415

acts_as_taggable_on finding tags of associated model

In my Rails app, I have a model called Projects and projects have man Spins. I'm using the gem acts_as_taggable_on to enable users to add tags to their Spins.

I'd like to be able to filter projects by tags, so if a project has any spin that contains the particular tag, I'd like to return that project in search results.

I know that I can search for Spins that have particular tags with the following:

Spin.tagged_with("LEGO")

How can I do an associated search, something like

Project.find(1).spins.where(:tagged_with => "LEGO")

(this doesn't work because there is no tagged_with attribute of Spins – acts_as_taggable_on adds a separate table for tags).

Upvotes: 0

Views: 119

Answers (1)

AbM
AbM

Reputation: 7779

Project.joins(:spins).where('spins.id IN (?)', Spin.tagged_with("LEGO").select(:id))

Upvotes: 1

Related Questions