ruby on rails - एक मेजबान तालिका के साथ एक बहु मॉडल टैग कैसे करें?
ruby-on-rails tags (1)
मेरी राय में, आप पोलीमोर्फ़िंग का इस्तेमाल कर सकते हैं कृपया सक्रिय रिकॉर्ड संघ देखें
आपके मामले में, मॉडल अगला हो सकता है:
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
....
और माइग्रेशन में:
create_table :tags , force: true do |t|
t.references :taggable, polymorphic: true, index: true
t.timestamps null: false
end
इसके बाद आप यह कर सकते हैं:
@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
मेरे पास एक टेबल शामिल है
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
जिसका उद्देश्य कई मॉडल के लिए एक टैग_ क्लाउड काम करना है मैं इसे application_controller में डाल दिया
def tag_cloud
@tags = CombineTag.tag_counts_on(:tags)
end
मेरा टैग_ क्लाउड ऐसा दिखता है:
<% 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 %>
मेरे पास सभी मॉडलों के नाम में यह रेखा है: <%= f.text_field :tag_list %>
combine_tags_helper
module CombineTagsHelper
include ActsAsTaggableOn::TagsHelper
end
मॉडल के
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
मुझे आपकी मदद करने में आपकी सहायता करने के लिए आपको अधिक स्पष्टीकरण या कोड की आवश्यकता है, तो कृपया मुझे बताएं :)