要麼如果你需要從頭開始寫,或者使用已經開發的寶石,你需要探索Polymorphic Associations
你可以學習任何acts_as_taggable_on或acts_as_taggable_on_steroids代碼。
這是migration code用於acts_as_taggable_on作爲一個例子
class ActsAsTaggableOnMigration < ActiveRecord::Migration
def self.up
create_table :tags do |t|
t.column :name, :string
end
create_table :taggings do |t|
t.column :tag_id, :integer
t.column :taggable_id, :integer
t.column :tagger_id, :integer
t.column :tagger_type, :string
# You should make sure that the column created is
# long enough to store the required class names.
t.column :taggable_type, :string
t.column :context, :string
t.column :created_at, :datetime
end
add_index :taggings, :tag_id
add_index :taggings, [:taggable_id, :taggable_type, :context]
end
def self.down
drop_table :taggings
drop_table :tags
end
end
謝謝!有用! – Dmitry 2011-04-07 19:07:33
謝謝,祝你工作順利。如果它在stackoverflow中工作,你需要打勾我的答案或在leas upvote它。 – tommasop 2011-04-08 06:07:18
當然!我也是新來的,所以我不能提出答案,但我勾選了它。 – Dmitry 2011-04-09 14:01:38