06. Rails console - max-borisov/ihub-bookshelf GitHub Wiki

Lets view data we created so far.

If course we can connect to the database and view databases, tables and rows. But there is anothe way how to fetch and manipulate with data as simple ruby objects using rails console command.

Open terminal and type the following:

$ rails console

Now you are able to run commands:

Rails console

Get list of users:

ap User.all

Here is what you get:

Get users in rails console

ap - is a shortcut for the awesome_print gem which shows objects in more friendly way.

Now you can view/create/delete/update active record models and so on.

Play around with these commands:

Find user with id 1

ap User.find(1)

Get all books

ap Book.all

Get first book from the list

ap Book.first

Get last review

ap Review.last

Get first book title

ap Book.first[:title]

Check is a last user admin

ap User.last.admin?

Destroy all orders

ap Order.destroy_all

Destroy first book

ap Book.first.destroy

Get amount of users

ap User.count

Helpful links