Ruby on rails 如何在rails 4上生成模型字段?

Ruby on rails 如何在rails 4上生成模型字段?,ruby-on-rails,Ruby On Rails,我跑了 rails generate Model Profile name:string 它已经运行并生成了所有文件。 当我看到profile.rb文件时,虽然它只有类声明而没有字段 class Profile < ActiveRecord::Base end 类配置文件

我跑了

rails generate Model Profile name:string
它已经运行并生成了所有文件。 当我看到profile.rb文件时,虽然它只有类声明而没有字段

class Profile < ActiveRecord::Base
end
类配置文件

我必须手动添加所有字段吗?

不,您不需要手动添加字段。ActiveRecord将在运行时从表结构中发现它们。这是rails魔力的一部分:)

您可以在名为
db/migrate/uu2; rb
的文件中添加字段。然后,当创建表并运行Rails时,表中的字段将自动可用(如Sergio所述)