1

私はRailsの初心者です。私の問題は次のとおりです。ユーザー用のテーブルがあり、さらにフィールドを追加する必要があります...

どこに置けばいいの?移行ファイルに入れようとしましたが、を実行してもスキーマが変更されませんrake db:migrate

これは私の移行ファイルにあります:

def self.up
 create_table :users do |t|
  t.string :username,         :null => false  # if you use another field as a username, for example email, you can safely remove this field.
  t.string :email,            :default => nil # if you use this field as a username, you might want to make it :null => false.
  t.string :crypted_password, :default => nil
  t.string :salt,             :default => nil
  t.string :nombres,          :default => nil
  t.string :apellidos,        :default => nil
  t.string :codigo,           :default => nil
  t.string :fecha,            :default => nil
  t.string :zona,             :default => nil
  t.string :institucion,      :default => nil
  t.string :frecuencia,       :default => nil
  t.string :pregunta,         :default => nil
  t.string :respuesta,        :default => nil



  t.timestamps
  end

そして、スキーマにはまだ新しいフィールドがありません

create_table "users", :force => true do |t|
t.string   "username",                     :null => false
t.string   "email"
t.string   "crypted_password"
t.string   "salt"
t.string   "nombres"
t.string   "apellidos"
t.string   "codigo"
t.string   "fecha"
t.string   "zona"
t.string   "institucion"
t.string   "frecuencia"
t.datetime "created_at",                   :null => false
t.datetime "updated_at",                   :null => false
t.string   "remember_me_token"
t.datetime "remember_me_token_expires_at"
end

私は何をすべきか?

4

2 に答える 2

2

簡単な移行

移行ジェネレーターを使用できます:
$> rails g migration add_position_to_users position:integer
次に実行します
$> rake db:migrate

より複雑な移行

またはレールが提供するより複雑な移行:
$> rails g migration add_body_and_pid_to_users body:string:index pid:integer:uniq:index
$> rake db:migrate

移行の詳細については、railsguideshttp://guides.rubyonrails.org/migrations.htmlを 参照してください。

于 2013-02-05T16:04:01.327 に答える
1

ここで言及していないのは、移行ファイルにコードを追加することだと思います。

列を追加するための私の手順は、次のようになります。

# rails generate migration AddFieldName blah:string

生成された移行ファイル内:

(ところで、これは通常次のようになります:db / migrations / 20130330115915_add_field_name.rb)

class AddFieldName < ActiveRecord::Migration
    def change
        add_column :table_name, :blah, :string
    end
end

この変更を行った後、db:migrateを実行します。次に、列がデータベースに追加されます。

于 2013-03-30T12:11:26.673 に答える