67
61

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsで開発している時によく使う/たまに使うメソッド集

Posted at

try

nil.aaa
 # => NoMethodError: undefined method `aaa' for nil:NilClass
nil.try(:aaa)
 # => nil

blank?

nil.blank?
 # => true

''.blank?
 # => true

[].blank?
 # => true

{}.blank?
 # => true

present?

nil.present?
 # => false

''.present?
 # => false

[].present?
 # => false

{}.present?
 # => false

presence

nil.presence
 # => nil

''.presence
 # => nil

[].presence
 # => nil

{}.presence
 # => nil

'a'.presence
 # => "a"

1.presence
 # => 1

SecureRandom

SecureRandom.uuid
 # => "c73273d4-68cb-42cd-b454-7478e9af4734"
SecureRandom.hex(16)
 # => "3f4a73bd984ccf7947f66109674b1e96"
SecureRandom.hex(8)
 # => "10b7662204013166"

classify/underscore

'static_pages'.classify
 # => "StaticPage"

'StaticPage'.underscore
 # => "static_page"

Hash#slice/Hash#except

{ a: 1, b: 2, c: 3 }.slice(:a, :c)
 # => {:a=>1, :c=>3}

{ a: 1, b: 2, c: 3 }.except(:a, :c)
 # => {:b=>2}

attributes/to_json/JSON.parse - ARオブジェクトを変換

  • ARオブジェクト => Hash
User.last.attributes.class
 # => Hash
  • Hash => JSON
User.last.attributes.to_json.class
 # => String
  • JSON => Hash
JSON.parse(User.last.attributes.to_json).class
 # => Hash
  • ARオブジェクト => JSON
User.last.to_json.class
 # => String

SHA1/SHA256/SHA384/SHA512 - ハッシュ値生成

Digest::SHA1.hexdigest('test')
 # => "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"

Digest::SHA2.hexdigest('test')
 # => "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"

Digest::SHA256.hexdigest('test')
 # => "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"

Digest::SHA384.hexdigest('test')
 # => "768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9"

Digest::SHA512.hexdigest('test')
 # => "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"

xxx_ids - 関連テーブルのID一覧を取得

Plan.last.days.pluck(:id)
 # => [13468, 13469, 13470, 13471, 13472, 13473, 13474, 13475]

↓同様の実行結果

Plan.last.day_ids
 # => [13468, 13469, 13470, 13471, 13472, 13473, 13474, 13475]

symbolize_keys/deep_symbolize_keys/stringify_keys

{ "a" => "b" }.symbolize_keys
 # => {:a=>"b"}

{ 'person' => { 'name' => 'Rob', 'age' => 28 } }.deep_symbolize_keys
 # => {:person=>{:name=>"Rob", :age=>28}}

{ a: "b" }.stringify_keys
 # => {"a"=>"b"}

find_each/find_in_batches

Person.find_each do |person|
  person.do_awesome_stuff
end

Person.where("age > 21").find_each do |person|
  person.party_all_night!
end
Person.where("age > 21").find_in_batches do |group|
  sleep(50) # Make sure it doesn't get too crowded in there!
  group.each { |person| person.party_all_night! }
end

index_by

people.index_by(&:login)
 # => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
people.index_by { |person| "#{person.first_name} #{person.last_name}" }
 # => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
67
61
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
67
61

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?