Copy Rails Console output to a file to debug it later
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)
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.
At BoTree Technologies, we build enterprise applications with our RoR team of 25+ engineers.
Consulting is free – let us help you grow!
Choose Your Language
Subscribe to Our Newsletter
- Digital Marketing
- IT Consulting
- Project Management