If you are using Rails muti-database feature, it's quite useful to know if you…
This one is a simple quality-of-life improvement.
In Rails 6.0, rails routes command will support --expanded (or -x, the shorthand version). This is similar to \x mode in psql where it will print the record in detailed list instead of in a table.
rails routes
--expanded
-x
\x
psql
$ rails routes --expanded--[ Route 1…
Back in 2012, Rails 3.2.0 added a .pluck method which you can call on a ActiveRecord::Relation or a model class:
.pluck
ActiveRecord::Relation
> User.pluck(:id)#=> [1, 2, 3]
But what if you need just the first value from the result? You will probably have to…
I came across this change when I did a talk at Tokyo Rubyist Meetup a few months ago.
I recently having a discussion with my colleague about a class variable in one of the pull request. He proposed a commit which includes a code that uses a class variable. I suggested that we should use a Thread.current to store that value instead for thread…
Thread.current
If you are using Pry in your project, you might run into situation where you are in a loop or having multiple binding.pry and so you have to use exit multiple times to get out from it.
binding.pry
exit
It’s pretty common in Ruby on Rails to use find_or_create_by method when you want to create a new record if the record isn’t already existed:
find_or_create_by
> User.find_or_create_by(username: "sikachu") User Load…
When you are developing a complex Ruby on Rails application, you might need to write a few SQL queries and avoid using Active Record and ARel to improve your application’s throughput. It’s pretty common to see them written…