【问题标题】:Equivalent RoR code for MySQL codeMySQL 代码的等效 RoR 代码
【发布时间】:2012-01-09 12:18:42
【问题描述】:

以前我用这些语句在 MySQL 中创建了数据库和表:

CREATE DATABASE amiref;
USE amiref;
CREATE TABLE refoo
(
  f1 VARCHAR(20) ,
  f2 VARCHAR(30) NOT NULL ,
  f3 INT ,
  PRIMARY KEY(f1)
);
CREATE TABLE IF NOT EXISTS users
(
  user_id1 VARCHAR(20) NOT NULL ,
  user_id2 VARCHAR(50) ,
  password VARCHAR(30) ,
  email VARCHAR(50) ,
  PRIMARY KEY(user_id1,user_id2)
);

知道我想用模型在 ruby​​ on rail 中创建这些数据库和表。我该怎么做? 请帮我。 谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 activerecord activemodel


    【解决方案1】:

    您使用迁移来执行此操作。这是您应该从头开始学习的真正基础知识!您将在此处找到文档:

    http://guides.rubyonrails.org/migrations.html

    使用脚手架创建模型时会包含迁移 => http://guides.rubyonrails.org/getting_started.html#getting-up-and-running-quickly-with-scaffolding

    //也提高你的接受率!

    迁移示例:

    创建迁移 =>

    rails g migration testMigration
    

    然后你会在db/migrate 中找到迁移。要创建表,请添加:

    create_table :table_name do |f|
         f.integer :integer_column1
         f.string :string_column1, :string_column2
         f.boolean :boolean_column1
    end
    

    然后运行迁移

    bundle exec rake db:migrate
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      相关资源
      最近更新 更多