7

Ho un join tavoloCome creare un tag_cloud multi-modello con una tabella di join?

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 

il cui scopo è di fare un lavoro tag_cloud per più modelli. Ho messo questo nella application_controller

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

mio tag_cloud assomiglia a questo:

<% 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 %> 

ho questa linea nella _form di tutti i modelli: <%= f.text_field :tag_list %>

combine_tags_helper

module CombineTagsHelper 
    include ActsAsTaggableOn::TagsHelper 
end 

modelli

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 

prega fatemi sapere se avete bisogno di ulteriori spiegazioni o code per aiutarvi a me :) aiutare

+0

Mi puoi dire in breve, quale è l'esatto comportamento che stai cercando di implementare? –

+0

So come creare un tag_cloud con un modello, ma non riesco a farlo funzionare con più modelli, dove se ho creato un tag chiamato 'run' sotto abitudini e sotto obiettivi che tag_cloud rappresenterebbe l'utilizzo del tag in proporzione. Questo aiuta @ArupRakshit? –

+0

puoi informarmi sull'usecase? perché hai bisogno di _join_table_ cercando di capire? –

risposta

1

A mio parere, è possibile utilizzare il polimorfo. Si prega di vedere Active Record Associations

Nel tuo caso, modello potrebbe essere il prossimo:

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

E in migrazioni:

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

Dopo questo è possibile:

@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