Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
60
Help us understand the problem. What is going on with this article?
@shu_0115

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

More than 5 years have passed since last update.

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 ...>, ...}
60
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
shu_0115
Web Engineer

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
60
Help us understand the problem. What is going on with this article?