ruby on rails:如何将数据库更改带到模型

时间:2023-01-14 14:28:44

I have update migrate script under db/migrate, and I did a

我在db / migrate下有更新迁移脚本,我做了一个

rake db:migrate

database script before update

更新前的数据库脚本

class CreateStudents < ActiveRecord::Migration
  def change
    create_table :students do |t|
      t.string :firstname
      t.string :lastname      
      t.string :account
      t.timestamps
    end
  end
end 

databse script after update

更新后的数据库脚本

class CreateStudents < ActiveRecord::Migration
  def change
    create_table :students do |t|
      t.string :firstname
      t.string :lastname      
      t.string :account
      t.string :address      
      t.string :city
      t.string :state
      t.string :postcode                        
      t.string :homephone
      t.timestamps
    end
  end
end 

after I dropped the old development.sqlite3 and old schema in schame.rb.
Say I added a few columns, but in the model these columns is missing.

我在schame.rb中删除了旧的development.sqlite3和旧模式之后。假设我添加了几列,但在模型中缺少这些列。

But my model still is

但我的模型仍然是

class Student < ActiveRecord::Base
  attr_accessible :firstname,:lastname,:account,
end

Is there a easy way I can bring the changes in new migrate script to model ?

有没有一种简单的方法可以将新迁移脚本中的更改带入模型?

3 个解决方案

#1


1  

It looks like maybe you did rails generate migration which isn't meant to affect your model. I believe after you create your model everything afterward has to be done manually.

看起来好像你做了rails生成迁移,这并不意味着影响你的模型。我相信在您创建模型之后,所有事情都必须手动完成。

If you really want to effect changes to your database and model at the same time, your best bet might be to delete your migrations and model and do a rails generate scaffold (documentation) to create your entire scaffolding from scratch.

如果您真的想同时对数据库和模型进行更改,最好的办法是删除迁移和模型,并执行rails生成脚手架(文档)以从头开始创建整个脚手架。

#2


2  

If you want to allow for mass assignments of the other attributes, you can just add the keys to attr_accessible

如果要允许其他属性的批量分配,只需将键添加到attr_accessible即可

class Student < ActiveRecord::Base
  attr_accessible :firstname,:lastname,:account,:address, :city, :state, :postcode, :homephone
end

However, your model still has those attributes (or columns as you call them). You just can't do a mass assignment (like create or update_attributes) without making them attr_accessible first.

但是,您的模型仍然具有这些属性(或您调用它们的列)。你不能先进行质量分配(比如create或update_attributes)而不先让它们成为attr_accessible。

#3


0  

There are no problem to add the new columns manually in the model.

在模型中手动添加新列没有问题。

#1


1  

It looks like maybe you did rails generate migration which isn't meant to affect your model. I believe after you create your model everything afterward has to be done manually.

看起来好像你做了rails生成迁移,这并不意味着影响你的模型。我相信在您创建模型之后,所有事情都必须手动完成。

If you really want to effect changes to your database and model at the same time, your best bet might be to delete your migrations and model and do a rails generate scaffold (documentation) to create your entire scaffolding from scratch.

如果您真的想同时对数据库和模型进行更改,最好的办法是删除迁移和模型,并执行rails生成脚手架(文档)以从头开始创建整个脚手架。

#2


2  

If you want to allow for mass assignments of the other attributes, you can just add the keys to attr_accessible

如果要允许其他属性的批量分配,只需将键添加到attr_accessible即可

class Student < ActiveRecord::Base
  attr_accessible :firstname,:lastname,:account,:address, :city, :state, :postcode, :homephone
end

However, your model still has those attributes (or columns as you call them). You just can't do a mass assignment (like create or update_attributes) without making them attr_accessible first.

但是,您的模型仍然具有这些属性(或您调用它们的列)。你不能先进行质量分配(比如create或update_attributes)而不先让它们成为attr_accessible。

#3


0  

There are no problem to add the new columns manually in the model.

在模型中手动添加新列没有问题。