Rails 3向表中添加字段 [英] Rails 3 adding fields to a table

查看:205
本文介绍了Rails 3向表中添加字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ok我是一个noob,我想知道如何添加字段到rails 3中的迁移,另外我使用Typus,并想知道是否需要手动添加这些字段,或者我只是重新生成typus和

提前感谢



Robbie

解决方案

迁移用于向数据库添加字段,而不是告诉Rails关于字段。



Rails将实际检查表的字段,所以如果你有一个现有的表,你可以创建一个名为 spy.rb 的模型,它会知道<$ c中的所有字段$ c> spies



要使用迁移,请运行 rails generate migration AddScreenshotColumns 。那么该文件可以成为:

  class AddScreenshotColumnsToTemplate< ActiveRecord :: Migration 
def self.up
add_column:templates,:screenshot_file_name,:string
end

def self.down
remove_column:templates, :screenshot_file_name
end
end


ok i am a noob and i want to know how to add fields to a migration in rails 3, additionally i am using Typus and would like to know if i need to add these fields manually or can i just regenerate the typus and it will just pick the new fields up?

Thanks in advance

Robbie

解决方案

Migrations are used to add fields to the database, not tell Rails about fields.

Rails will actually inspect the table for its fields, so if you have an existing table, you can create a model called spy.rb and it will know about all fields in spies

To use migrations, run rails generate migration AddScreenshotColumns. Then that file can become:

class AddScreenshotColumnsToTemplate < ActiveRecord::Migration
  def self.up
    add_column :templates, :screenshot_file_name,    :string
  end

  def self.down
    remove_column :templates, :screenshot_file_name
  end
end

这篇关于Rails 3向表中添加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆