RailsでDBを更新してみる

RailsをMySQLに対応させてみるの続き
RailsのMySQLの環境にアクセスできるようにしておくも済ませておく)

Railsでいろいろ更新してみる。


とりあえずUserのモデルとテーブルを作っておく。

$ rails generate model user name:string point:integer flag:boolean day:date
$ rake db:migrate

追加

newで作成し、saveで追加できる。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    user = User.new(name: '佐藤', point: 100, flag: true, day: '2001/1/1')
    result = user.save
    render :json => user
  end
end

createだとnewとsaveをまとめて行える。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    user = User.create(name: '鈴木', point: 200, flag: false, day: '2002/2/2')
    render :json => user
  end
end

activerecord-importを使うとbulk insertができる。

$ vi Gemfile
gem 'activerecord-import'	←追加
$ bundle install
$ sudo service httpd restart
apacheの再起動が必要?
$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    users = []
    users << User.new(name: '高橋', point: 300, flag: true,  day: '2003/3/3')
    users << User.new(name: '田中', point: 400, flag: false, day: '2004/4/4')
    users << User.new(name: '伊藤', point: 500, flag: true,  day: '2005/5/5')
    User.import(users)
    render :json => users
  end
end

上記の処理を実行すると、DBの状態は下記のようになる。

+----+--------+-------+------+------------+---------------------+---------------------+
| id | name   | point | flag | day        | created_at          | updated_at          |
+----+--------+-------+------+------------+---------------------+---------------------+
|  1 | 佐藤   |   100 |    1 | 2001-01-01 | 2014-01-15 11:40:08 | 2014-01-15 11:40:08 |
|  2 | 鈴木   |   200 |    0 | 2002-02-02 | 2014-01-15 11:41:21 | 2014-01-15 11:41:21 |
|  3 | 高橋   |   300 |    1 | 2003-03-03 | 2014-01-15 11:43:43 | 2014-01-15 11:43:43 |
|  4 | 田中   |   400 |    0 | 2004-04-04 | 2014-01-15 11:43:43 | 2014-01-15 11:43:43 |
|  5 | 伊藤   |   500 |    1 | 2005-05-05 | 2014-01-15 11:43:43 | 2014-01-15 11:43:43 |
+----+--------+-------+------+------------+---------------------+---------------------+

更新

検索して、値を設定し、saveすると値を変更できる。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    user = User.find(1)
    user.point = '101'
    user.save
    render :json => user
  end
end

update_attributeだと値の設定とsaveをまとめて行える。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    user = User.find(2)
    user.update_attribute(:point, '202')
    render :json => user
  end
end

複数のカラムを変更したい場合はupdate_attributes。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    user = User.find(3)
    user.update_attributes(:point => '303', :flag => false)
    render :json => user
  end
end

updateだとfindとupdate_attributesをまとめて行える。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    user = User.update(4, :point => '404', :flag => true)
    render :json => user
  end
end

update_allを使うと複数行変更できる複数行変更できる。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    result = User.where(:flag => true).update_all(:day => '2006-06-06')
    render :text => result 
  end
end
戻り値は更新した件数?

上記の処理を実行すると、DBの状態は下記のようになる。

+----+--------+-------+------+------------+---------------------+---------------------+
| id | name   | point | flag | day        | created_at          | updated_at          |
+----+--------+-------+------+------------+---------------------+---------------------+
|  1 | 佐藤   |   101 |    1 | 2006-06-06 | 2014-01-15 11:40:08 | 2014-01-15 11:45:31 |
|  2 | 鈴木   |   202 |    0 | 2002-02-02 | 2014-01-15 11:41:21 | 2014-01-15 11:46:22 |
|  3 | 高橋   |   303 |    0 | 2003-03-03 | 2014-01-15 11:43:43 | 2014-01-15 11:46:42 |
|  4 | 田中   |   404 |    1 | 2006-06-06 | 2014-01-15 11:43:43 | 2014-01-15 11:47:10 |
|  5 | 伊藤   |   500 |    1 | 2006-06-06 | 2014-01-15 11:43:43 | 2014-01-15 11:43:43 |
+----+--------+-------+------+------------+---------------------+---------------------+

削除

deleteで削除できる。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    result = User.delete(2)
    render :text => result
  end
end

delete_allで複数行削除できる。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    result = User.where(:flag => true).delete_all()
    render :text => result
  end
end
こいつの返却値は何だ?

上記の処理を実行すると、DBの状態は下記のようになる。

+----+--------+-------+------+------------+---------------------+---------------------+
| id | name   | point | flag | day        | created_at          | updated_at          |
+----+--------+-------+------+------------+---------------------+---------------------+
|  3 | 高橋   |   303 |    0 | 2003-03-03 | 2014-01-15 11:43:43 | 2014-01-15 11:46:42 |
+----+--------+-------+------+------------+---------------------+---------------------+