yuan 发表于 2013-2-8 00:54:58

Acts As Taggable On Steroids

参考着mephisto写blog程序,第一次看到[].collect(&:name)的写法,别笑我……书上没见过,代码又写得少,结果就是这样。
=============================================
答案在这里:http://www.infoq.com/cn/articles/ruby-open-classes-monkeypatching


主要记录一下acts_as_taggable_on_steroids的用法。
http://agilewebdevelopment.com/plugins/acts_as_taggable_on_steroids

首先,安装这个插件到项目中:
ruby script/plugin install git://github.com/jviney/acts_as_taggable_on_steroids.git

使用
接着,执行:
ruby script/generate acts_as_taggable_on_steroids_migration
会生成如下migration代码:
class ActsAsTaggableMigration < ActiveRecord::Migrationdef 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      # You should make sure that the column created is      # long enough to store the required class names.      t.column :taggable_type, :string      t.column :created_at, :datetime    end    add_index :taggings, :tag_id    add_index :taggings, [:taggable_id, :taggable_type]enddef self.down    drop_table :taggings    drop_table :tagsendend
这里建了两个表,tag表和taggings表。taggings表看着像是个多对多的连接表,另外多了个taggable_type字段,估计是要做成不同类型的taggable对象都能通用的吧。
然后……只要在需要tag的类里边加一句acts_as_taggalbe的调用就可以了……自己不用插件瞎搞了大半天的东东,两三个命令加一句代码解决了。嗯?居然没有Tag类,估计隐藏在插件内部了。grep一下:
grep 'class Tag' ./ -r
果然……
<div class="quote_title">引用
页: [1]
查看完整版本: Acts As Taggable On Steroids