Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing an array or object in Ruby on rails. Also view the structure of the data

I'm executing an Active Record query in controller. And now I want to view all of its contents weather it is in the form of array or object. I want to see the structure in which the data is being returned. I'm new to ruby on rails. In PHP we use var_dump() or print_r().

like image 471
Sachin Prasad Avatar asked Oct 22 '12 12:10

Sachin Prasad


Video Answer


1 Answers

There are a couple of ways to do this. If you want to play with the result interactively, open the rails console by typing rails console. Run the query you want in the console

query_result = MyModel.find_by_interesting_parameter( 'Foo' )

Then use the to_yaml method to dump a nice structure out

puts query_result.to_yaml

Sometimes, it's just easier to see what the view has had back. To do this, use the debug method in the view itself...

<%= debug @post %>

See this page here for more information

like image 139
razorhead Avatar answered Sep 18 '22 02:09

razorhead