zoonoo
zoonoo

Reputation: 525

rails : How to query based on acts_as_taggable_on tags of association?

You can find tagged objects using tagged_with.

class User < ActiveRecord::Base
  acts_as_taggable_on :tags, :skills
  scope :by_join_date, order("created_at DESC")
end

User.tagged_with("awesome").by_join_date

But how do you find the associations of tagged objects?

class UserAccount < ActiveRecord::Base
  belongs_to :user
end

UserAccount.joins(:user)...???

Upvotes: 0

Views: 387

Answers (1)

unkmas
unkmas

Reputation: 987

UserAccount.joins(:user).merge(User.tagged_with("awesome"))

Or you can use reverse query:

User.tagged_with("awesome").includes(:user_account).

Query selection depends on your goal.

Upvotes: 1

Related Questions