Search code examples
ruby-on-railsactiverecordacts-as-taggable-on

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)...???

Solution

  • 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.