Recently, a client asked us to give emails of all the customers whose billing and shipping address is India.

As a Ruby on Rails developer, we generally jump on rails console in production, fire the query and fetch the results.

The query was easy but the number of emails were huge. We have to scroll down for about 2-3 min to reach to the end in production rails console.

Sometimes, it would be difficult to manually copy and paste the result by doing scrolling. In this case, there is a risk of data loss when the amount of data is large.

We wanted to get the output of the query from console to file and then copy that file to local server using secure copy (scp). This way we can easily convert the file to CSV and send the file to the client in less than 2 minutes.

Below is the way to copy the console output to the file.

f = File.new("emails.txt", 'w')

f << User.joins(:address).where(‘addresses.country =?’, ‘India’).pluck(:email)

f.close

Here we have created a file with write permission, inserted query output to the file and then closed it.

You can see the file in your project home directory.

This is how we have solved one generic problem in a pragmatic way.

Pro Tip: Always use sandbox mode while getting information/debugging issues on production console.

Read Also: How to send responsive Emails using MJML in Rails?

Click here for more details…


At BoTree Technologies, we build enterprise applications with our RoR team of 25+ engineers.

We also specialize in RPA, AI, Python, Django, JavaScript and ReactJS.

Consulting is free – let us help you grow!