LoginSignup
55
27

More than 5 years have passed since last update.

RailsでActiveRecordのデータをhashにする方法

Last updated at Posted at 2015-04-15

.to_hも.to_hashも使えないので調べてみたら
.attributesで取得できるみたい。

例えば、Userという名前のモデル(usersテーブル)

user = User.first
user.attributes
# => {"id"=>1,
# "name"=>"なまえ",
# "created_at"=>Mon, 15 Dec 2014 08:22:33 JST +09:00,
# "updated_at"=>Wed, 15 Apr 2015 16:02:45 JST +09:00}
55
27
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
55
27