2011-04-06 57 views
0

我正在編寫我的學生項目,並使用基於社交網絡功能的lovdbyless引擎。
我想爲照片添加標籤支持。我是新手,所以問題是:
是否有正確的步驟?
1.添加使用id和name屬性在數據庫中創建'tags'表的遷移,以及具有tag_id和photo_id字段的'tagsphotos'表。
2.爲照片創建具有'has_and_belongs_to_many'的標籤的模型 3.爲照片模型中的標籤添加'has_and_belongs_to_many'。 4.現在開始從控制器使用這些東西。爲lovdbyless上的照片添加標籤

這個組織是否有效? 非常感謝!

回答

0

要麼如果你需要從頭開始寫,或者使用已經開發的寶石,你需要探索Polymorphic Associations

你可以學習任何acts_as_taggable_onacts_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 
+0

謝謝!有用! – Dmitry 2011-04-07 19:07:33

+0

謝謝,祝你工作順利。如果它在stackoverflow中工作,你需要打勾我的答案或在leas upvote它。 – tommasop 2011-04-08 06:07:18

+0

當然!我也是新來的,所以我不能提出答案,但我勾選了它。 – Dmitry 2011-04-09 14:01:38