LoginSignup
7
7

More than 5 years have passed since last update.

ハッシュから配列で指定したキーの要素だけを取り出す

Last updated at Posted at 2013-06-09

例えば

@post = Post.new
temp = JSON.parse(@post.to_json)
#=> {id:nil, title:nil, body:nil, created_at:nil, updated_at:nil}

Post.new(tmp)

ってやるとidcreated_at, updated_atなどがMass assignment securityに引っかかる
必要なものだけのHash(titlebodyだけ)にしてやりたいなら、ActiveSupportのHash#sliceをつかう

Post.new(tmp.slice(*Post::accessible_attributes) #=> true
7
7
2

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
7
7