Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails console - display active record results in a table

Is there a way to display Active Record results in table format in the script/console environment?

like image 989
Power Point Avatar asked Sep 29 '10 23:09

Power Point


People also ask

What is ActiveRecord in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.

What is ActiveRecord base?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.


2 Answers

Yep. There's a gem called hirb that does this very well.

like image 80
Jordan Running Avatar answered Oct 01 '22 16:10

Jordan Running


Also check out http://tableprintgem.com. It's an efficient way to view a list of structured data, making it easy to scan and compare across large swaths of records (and for many people, it's a comfortable return to the SQL command line output of yesteryear :)

table_print ruby gem - explore your data

The most powerful feature of table_print is the ability to see your data in the context of other objects it relates to. You can reference nested objects with the method chain required to reach them. This example is showing data from three different tables:

  • name from the Author table (reached through author.name)
  • title from the Book table (reached through author.books.title)
  • caption from the Photo table (reached through author.books.photos.caption)

table_print ruby gem - contextualize your data

There's a short intro screencast at http://tableprintgem.com

(full disclosure: I wrote this gem)

like image 20
Chris Doyle Avatar answered Oct 01 '22 17:10

Chris Doyle