【问题标题】:Active Record: Change the values of certain items in the database through terminalActive Record:通过终端更改数据库中某些项目的值
【发布时间】:2015-07-25 01:46:58
【问题描述】:

假设我已经创建了这个表:

class CreateTweets < ActiveRecord::Migration
  def up
    create_table :contents do |t|
      t.string :user_id
      t.string :content
      t.timestamps
    end
  end
  def down
    drop_table :contents
  end
end

如何通过终端更改我拥有 ID 的特定内容的 user_id 的值并将其保存以用于测试目的。

非常感谢。

【问题讨论】:

    标签: ruby-on-rails database activerecord


    【解决方案1】:

    将id为1的内容更新为user_id为5(假设user_id是一个整数字段):

    运行rails console,然后运行Content.update(1, user_id: 5)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 2019-06-30
      相关资源
      最近更新 更多