2011-04-06 56 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