Skip to content

Instantly share code, notes, and snippets.

@taea
Last active December 17, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taea/5575855 to your computer and use it in GitHub Desktop.
Save taea/5575855 to your computer and use it in GitHub Desktop.
rails c でデータを入れる
$ rails c

User モデルの一個目のデータを"u" に代入(モデルは大文字はじまり・単数)

irb(main):000:0> u = User.first

↓ 一個目出る

=> #<User id: 1, company_id: 1, group_id: nil, name: "ken_c_lo", title: "後楽園ゆうえんちで僕と握手", age: nil, profile: nil ・・・・>

データはvalidですかね? -> うんにゃ

irb(main):000:0> u.valid?
=> false

エラーは何? -> @messages 以降の部分がエラー(未入力の必須項目が確認できる)

irb(main):000:0> u.errors

=> #<User id: 1, company_id: 1, group_id: nil, name: "ken_c_lo", title: "後楽園ゆうえんちで僕と握手", age: nil, profile: nil ・・・・ >, @messages={:profile=>["を入力してください。"]}>

profile が入ってないとだめらしいのでprofileいれる

irb(main):000:0> u.profile = "よろしくよろしくよろしくです"

=> "よろしくよろしくよろしくです"

データはvalidですかね? -> おk

irb(main):000:0> u.valid?
=> true

データ揃ったらしいので保存

irb(main):000:0> u.save
   (0.1ms)  BEGIN
   (1.7ms)  UPDATE `users` SET `profile` = 'よろしくよろしくよろしくです', `updated_at` = '2013-05-14 20:25:06' WHERE `users`.`id` = 1
   (0.5ms)  COMMIT
=> true

ヾ('ω')ノ゙

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment