7

I have a join table

create_table "combine_tags", force: true do |t|
  t.integer "user_id"
  t.integer "habit_id"
  t.integer "valuation_id"
  t.integer "goal_id"
  t.integer "quantified_id"
end

whose purpose is to make a tag_cloud work for multiple models. I put this in the application_controller

def tag_cloud
  @tags = CombineTag.tag_counts_on(:tags)
end

My tag_cloud looks like this:

<% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
  <%= link_to tag.name, tag_path(tag), :class => css_class %>
<% end %>

# or this depending on which works:

<% tag_cloud CombineTag.tag_counts, %w[s m l] do |tag, css_class| %>
  <%= link_to tag.name, tag_path(tag.name), class: css_class %>
<% end %>

I have this line in the _form of all the models: <%= f.text_field :tag_list %>

combine_tags_helper

module CombineTagsHelper
  include ActsAsTaggableOn::TagsHelper
end

models

class CombineTag < ActiveRecord::Base
  belongs_to :habit
  belongs_to :goal
  belongs_to :quantified
  belongs_to :valuation
  belongs_to :user
  acts_as_taggable
end

class Habit < ActiveRecord::Base # Same goes for other models
  has_many :combine_tags
  acts_as_taggable
end

Please let me know if you need further explanation or code to help you help me :)

tereško
  • 58,060
  • 25
  • 98
  • 150
AnthonyGalli.com
  • 2,796
  • 5
  • 31
  • 80
  • Can you tell me in brief what the exact behavior you are looking for to implement ? – Arup Rakshit Jun 11 '15 at 03:29
  • I know how to make a tag_cloud with one model, but I can't get it to work with multiple models where if I created a tag called `run` under habits & under goals that the tag_cloud would represent the tag's usage in proportion. Does that help @ArupRakshit? – AnthonyGalli.com Jun 11 '15 at 03:37
  • can you brief me about the usecase ? why you need _join_table_ trying to understand ? – Arup Rakshit Jun 11 '15 at 04:10
  • @ArupRakshit I came to this attempt based on the suggestions here: http://stackoverflow.com/questions/29906256/how-to-use-multiple-models-for-tag-cloud so maybe you'll find this question more helpful. Ultimtaely I'm trying to create a tag_cloud using multiple models. I'm open to chatting if you need to. I'm available for the next hour :) – AnthonyGalli.com Jun 11 '15 at 04:21
  • can you join http://chat.stackoverflow.com/rooms/79959/r-o-r ? – Arup Rakshit Jun 11 '15 at 04:22

1 Answers1

1

In my opinion, You could use polimorphing. Please, see Active Record Associations

In Your case, Model could be next:

class Tag < ActiveRecord::Base
  belongs_to :taggable, polymorphic: true 
....

class Habit < ActiveRecord::Base
  has_many :tags, as: :taggable
....

class Goal < ActiveRecord::Base
  has_many :tags, as: :taggable
....

And in migrations:

create_table :tags , force: true do |t|
  t.references :taggable, polymorphic: true, index: true
  t.timestamps null: false    
end

After this You can:

@tags = Tag.include(:taggable)
@tags.each do |tag|
  type = tag.taggable_type # string, some of 'habit', 'goal' etc
  id = tag.taggable_id # id of 'habit', 'goal' etc
end