Command output in rails console: Explanation of unwanted output?

I work with a rail console and some models. I run things like:

Model.find(:all).each do |x| p x.name end

which is nice, it allows me to see all the values ​​of a certain column, but after it prints these lines, it prints the whole model.

Why is he doing this? How can I stop him?

+3
source share
1 answer

The console always prints the return value of the command. And the return value for .eachis the initial array.

So, you either return the desired value:

Model.find(:all).map{ |x| x.name }

Or prevent the output by returning something like nil:

Model.find(:all).each{ |x| p x.name }; nil
+12
source

Source: https://habr.com/ru/post/1743525/


All Articles