データベースに保存
適応バージョン
- 3.0.0
 - 3.0.5
 - 3.0.7
 - 3.0.9
 - 3.1.0
 - 3.2.3
 - 3.2.8
 - 3.2.13
 - 4.0.2
 - 4.1.8
 - 4.2.1
 - 4.2.7
 - 4.2.9
 - 5.0.0.1
 - 5.1.7
 - 5.2.3
 - 6.0.2.1
 - 7.0.0
 
説明
生成したモデルオブジェクトをデータベースに保存
使い方
モデル.save(オプション引数)
# 失敗したら例外発生
モデル.save!(オプション引数)
例
生成したモデルオブジェクトをデータベースに保存
user = User.new
user.name = "A"
user.save
新しいローカル変数を作らずに保存
User.new do |i|
  i.name = "A"
  i.save
end
属性ハッシュの保存
user = User.new(name: "A")
user.save
データベースへの保存の有無で処理を分ける
if @user.save
  redirect_to :list
else
  render action: "new"
end
複数の項目を保存
begin
  Entry.transaction do
    @many_entries.each { |entry| entry.save! }
  end
 vredirect_to :list
  rescue ActionRecord::RecordInvalid, ActionRecord::RecordNotSaved: ex
  render action: "input_multi_entries"
end
属性を変更して保存
@entry = Entry.find(params[:id])
@entry.message = "new Message"
@entry.save
複数の項目を1度に更新
@user = User.find(parms[:id])
@user.attributes = { username: 'Phusion', is_admin: true }
@user.save