GhostRider
GhostRider

Reputation: 2170

Rails 4: Using acts_as_taggable_on to link two models which have common tags

I have 2 models. A user model and a micropost model. Both of these models are taggable.

User.rb

 acts_as_taggable
 acts_as_taggable_on :tags

Micropost.rb

 acts_as_taggable
 acts_as_taggable_on :tags

The tags are enforced from a common list of 30 different "topics".

I am trying to find questions related to a users tags i.e. microposts that the user might be interested in.

Obviously to find users with common tags you can

   @users = @user.find_related_tags

But how can you do this across 2 models.

I have tried something stupid like

 micropost = Micropost.new
 micropost.tag_list = current_user.tag_list
 @questions_list = micropost.find_related_tags 

Where I hoped @questions_list would be an array of related microposts.This doesn't work

Any thoughts?

Upvotes: 1

Views: 308

Answers (1)

deefour
deefour

Reputation: 35360

What about something like

Micropost.tagged_with(@user.tag_list, :on => :tags, :any => true)

Based on this section from the docs.

Upvotes: 1

Related Questions