jellycola
jellycola

Reputation: 494

allow duplicate taggings using acts-as-taggable-on

Rails newbie here!

I am using acts-as-taggable-on to implement basic tagging, but I want to modify the default behaviour so that each instance of a model (say a post) can be tagged multiple times using the same tag.

@post.tag_list.add("awesome, awesome", parse: true) 

would only create one tag and one tagging in the default behavior. I would like it to use the same tag in the database but to create two unique taggings for that post.

Ultimately I would like to be able to count the number of times @post was tagged with "awesome" so I can make a tag frequency count for each post. What would be the best way to do this that wouldn't require rolling my own tag implementation?

I'm trying to add duplicate tags to a user. I want some user x to have multiple "awesome" tags. The default implementation wont let me.

Default implementation:

@instance.tag_list = "awesome, awesome, awesome"
@instance.save
@instance.reload
@instance.tags => 
[#<ActsAsTaggableOn::Tag id: 1, name: "awesome", taggings_count: 1>] 

I want taggings_count to return 3 instead, because I want to make 3 separate taggings to "awesome" even though they all refer to the same tag.

Upvotes: 4

Views: 694

Answers (1)

Pavel S
Pavel S

Reputation: 1543

This library already counts the tags. just look at attribute taggings_count of tag record. As from docs:

@user.tag_list = "awesome, slick, hefty"
@user.save
@user.reload
@user.tags
=> [#<ActsAsTaggableOn::Tag id: 1, name: "awesome", taggings_count: 1>,
 #<ActsAsTaggableOn::Tag id: 2, name: "slick", taggings_count: 1>,
 #<ActsAsTaggableOn::Tag id: 3, name: "hefty", taggings_count: 1>]

Taggings_coungs is the number of taggings applied.

Upvotes: 1

Related Questions