LoginSignup
3
3

More than 5 years have passed since last update.

[Rails 4.x] bulk insertをupdateでもしたいときにはupdate_allがあります

Posted at

bulk insertをしたい場合は、

gem 'activerecord-import'

を使うといいのですが、updateの場合は?

MySQLでは↑のgemでbulk updateも出来るようですが。。。(PostgresとかSqliteとか...)

update_all

速い話がこれで解決です。

# openがfalseのレコードをすべてtrueに変更したい
Shop.where(open: false).update_all(open: true)

# 全部のレコードをopen: trueに
Shop.update_all open: true

References

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3