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

3

5

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().

Rothman answered 22/10, 2012 at 12:10 Comment(1)
You can use rails c (rails console) and see sql query along with returned dataLovely
B
1

Take a look at this doc, 'Debugging Rails Applications', particularly section 3, 'Debugging with ruby-debug': http://guides.rubyonrails.org/debugging_rails_applications.html

Brittani answered 22/10, 2012 at 13:7 Comment(2)
Oh .. how comes i didn't see it. Thanks for the link that's what i wanted.Rothman
-1 for link-only answer. You should quote the pertinent content in your answer to avoid irrelevancy if/when link rot occurs.Woodworker
W
6

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

Witticism answered 22/10, 2012 at 13:19 Comment(0)
E
2

Try the to_yaml and the inspect methods.

Exobiology answered 22/10, 2012 at 12:13 Comment(0)
B
1

Take a look at this doc, 'Debugging Rails Applications', particularly section 3, 'Debugging with ruby-debug': http://guides.rubyonrails.org/debugging_rails_applications.html

Brittani answered 22/10, 2012 at 13:7 Comment(2)
Oh .. how comes i didn't see it. Thanks for the link that's what i wanted.Rothman
-1 for link-only answer. You should quote the pertinent content in your answer to avoid irrelevancy if/when link rot occurs.Woodworker

© 2022 - 2024 — McMap. All rights reserved.