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


## try

```rb
nil.aaa
 # => NoMethodError: undefined method `aaa' for nil:NilClass
```

```rb
nil.try(:aaa)
 # => nil
```

## blank?

```rb
nil.blank?
 # => true

''.blank?
 # => true

[].blank?
 # => true

{}.blank?
 # => true
```

## present?

```rb
nil.present?
 # => false

''.present?
 # => false

[].present?
 # => false

{}.present?
 # => false
```


## presence

```rb
nil.presence
 # => nil

''.presence
 # => nil

[].presence
 # => nil

{}.presence
 # => nil

'a'.presence
 # => "a"

1.presence
 # => 1
```

## SecureRandom

```rb
SecureRandom.uuid
 # => "c73273d4-68cb-42cd-b454-7478e9af4734"
```

```rb
SecureRandom.hex(16)
 # => "3f4a73bd984ccf7947f66109674b1e96"
```

```rb
SecureRandom.hex(8)
 # => "10b7662204013166"
```


## classify／underscore

```rb
'static_pages'.classify
 # => "StaticPage"

'StaticPage'.underscore
 # => "static_page"
```

## Hash#slice／Hash#except


```rb
{ 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

```rb
User.last.attributes.class
 # => Hash
```

* Hash => JSON

```rb
User.last.attributes.to_json.class
 # => String
```

* JSON => Hash

```rb
JSON.parse(User.last.attributes.to_json).class
 # => Hash
```

* ARオブジェクト => JSON

```rb
User.last.to_json.class
 # => String
```

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

```rb
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一覧を取得

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

↓同様の実行結果

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

## symbolize_keys／deep_symbolize_keys／stringify_keys

```rb
{ "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


* [find_each (ActiveRecord::Batches) - APIdock](http://apidock.com/rails/ActiveRecord/Batches/find_each)


```rb
Person.find_each do |person|
  person.do_awesome_stuff
end

Person.where("age > 21").find_each do |person|
  person.party_all_night!
end
```

* [find_in_batches (ActiveRecord::Batches) - APIdock](http://apidock.com/rails/v4.1.8/ActiveRecord/Batches/find_in_batches)

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

* [index_by (Enumerable) - APIdock](http://apidock.com/rails/Enumerable/index_by)

```rb
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 ...>, ...}
```