我正在尝试在我的数据库中创建大量标签,有谁知道如何使用 gem 来做到这一点?
产品表:
create_table :products do |t|
t.string :name
t.date :date
t.decimal :price, :default => 0, :precision => 10, :scale => 2
t.integer :user_id
end
并且该:tag_list
字段是由迁移创建的虚拟列ActsAsTaggableOn
:
class ActsAsTaggableOnMigration < ActiveRecord::Migration
def self.up
create_table :tags do |t|
t.string :name
end
create_table :taggings do |t|
t.references :tag
# You should make sure that the column created is
# long enough to store the required class names.
t.references :taggable, :polymorphic => true
t.references :tagger, :polymorphic => true
t.string :context
t.datetime :created_at
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
这是我的products/form.html.erb:tag_list
中的字段
<div class="field">
<%= f.label :tag_list %>:
<%= f.text_field :tag_list %>
</div>
我试图做这样的事情......
Product.create([
{:tag_list => 'Foods'},
{:tag_list => 'Electronics'},
{:tag_list => 'Pizza'},
{:tag_list => 'Groceries'},
{:tag_list => 'Walmart'},
{:tag_list => 'Apples'},
{:tag_list => 'Oranges'} ])
但是我缺乏 RoR 技能告诉我这是错误的方式,我需要帮助,有什么建议吗?