6

データベース内に大量のタグを作成しようとしていますが、gem act-as-taggable-on でこれを行う方法を知っている人はいますか?

製品表:

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 スキルの欠如は、これは間違った方法であり、助けが必要であると教えてくれます。何か提案はありますか?

4

2 に答える 2

13

あなたのseeds.rbでこれを試すことができます:

list = ['tag 1', 'tag 2', ...]

list.each do |tag|
  ActsAsTaggableOn::Tag.new(:name => tag).save
end

明らかに、リスト配列の値を目的のタグに置き換えます。

注: これは、タグ テーブルに入力するだけです。それがあなたが探していたものであることを願っています。

お役に立てれば!

于 2011-06-20T16:01:23.550 に答える