Runy-on-rails – the command output in the Rails console: No description of the output?

I am using rails console and some models. I am running something:

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

This is fine, it allows me to see all the values ​​of a specific column, but after printing out these lines, it prints out the entire model.

Why do you do this? How can I stop it?

The console always prints the return value of the command. The return value of .each is the initial array. < p>

So you either return the value you need:

Model.find(:all).map{ |x| x.name }< /pre> 

Or prevent the output and return something like nil:

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

I am using rails console and some models. I am running something:

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

This is fine, it allows me to see all the values ​​of a specific column, but after printing out these lines, it prints Out the entire model.

Why do you do this? How can I stop it?

The console always prints the return value of the command. And the return value of .each is the initial array.

So you either Return the value you need:

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

Or prevent the output, return something like nil stuff:

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

< /p>

Leave a Comment

Your email address will not be published.