# Using Tux in Sinatra with ActiveRecord

## Overview

In this lesson, we'll cover Tux, a Ruby gem, that you can use to make sure your database and ActiveRecord associations are set up properly.

## Objectives

* Describe the benefits of Tux
* Set up Tux locally
* Create, edit, delete, and update data

## What Is Tux

Tux is an incredible Ruby gem that lets you access your database and perform all CRUD operations on it through the terminal. It also loads a full environment in the console that allows you to see all routes and views. Primarily, you'll use Tux to make sure your database is set up properly, play around with Ruby objects, and make sure your ActiveRecord associations are working properly.

For example, in this repo there is a simple Sinatra app with a `User` model and a method called `say_name`. I want to test that the `say_name` method works. Wouldn't it be great if we could enter into some sort of playground environment where we can test out our code?

In the past, we've used IRB for just that. Let's try dropping into IRB and testing out our code.

Open up IRB in terminal and require the file `models/user.rb`:

[![require models/user.rb](https://camo.githubusercontent.com/ccf935cd79e67347d6745ac30fe7b1ca952ca758/687474703a2f2f726561646d652d706963732e73332e616d617a6f6e6177732e636f6d2f73696e617472612d7475782d6972622d6572726f722e706e67)](https://camo.githubusercontent.com/ccf935cd79e67347d6745ac30fe7b1ca952ca758/687474703a2f2f726561646d652d706963732e73332e616d617a6f6e6177732e636f6d2f73696e617472612d7475782d6972622d6572726f722e706e67)

Oh no! We get an error that it doesn't recognize `ActiveRecord`. Let's try to require the ActiveRecord gem: [![require activerecord](https://camo.githubusercontent.com/4c3851277cbb0b9c400e758b7a1dc5fe4ffa5c08/687474703a2f2f726561646d652d706963732e73332e616d617a6f6e6177732e636f6d2f73696e617472612d7475782d6972622d6572726f72322e706e67)](https://camo.githubusercontent.com/4c3851277cbb0b9c400e758b7a1dc5fe4ffa5c08/687474703a2f2f726561646d652d706963732e73332e616d617a6f6e6177732e636f6d2f73696e617472612d7475782d6972622d6572726f72322e706e67)

Trying to test our `User` model and methods in IRB is a big big mess. We keep running into issues. This is why Tux exists. By using the Tux gem, we can easily create objects, and test our methods to manipulate those objects.

## Setup

Setting up Tux is fairly simple. All you need to do is include it in your Gemfile and run `bundle install` in terminal.

## Using Tux

We've got a full Sinatra application with a single `User` class set up. We've already created the migration for you, but make sure you actually run the migration to create the user table.

Next, it's time to use Tux. In terminal in the directory of this walk-through enter `tux`. You should see something like this:

[![](https://camo.githubusercontent.com/be76b7ee50d61fb20c1d2807a77d74ceb13723c4/68747470733a2f2f637572726963756c756d2d636f6e74656e742e73332e616d617a6f6e6177732e636f6d2f7765622d646576656c6f706d656e742f53696e617472612f5475782e706e67)](https://camo.githubusercontent.com/be76b7ee50d61fb20c1d2807a77d74ceb13723c4/68747470733a2f2f637572726963756c756d2d636f6e74656e742e73332e616d617a6f6e6177732e636f6d2f7765622d646576656c6f706d656e742f53696e617472612f5475782e706e67)

The tux console has now loaded. Regular terminal commands won't work at this point, but you can use Ruby and ActiveRecord methods.

### Create

Just like in our controller action, we can create a user.

```
user = User.create(:name => "Trisha", :email => "trisha@test.com", :fav_icecream => "mint chocolate chip")
```

Or:

```
user = User.new
user.name = "Beth"
user.email = "beth@beth.com"
user.fav_icecream = "rocky road"
user.save
```

### Edit

We can edit a user that's already been saved to the database. Let's edit the first user.

```
user = User.first
user.name = "Trisha Yearwood"
user.save
```

### Delete

Now let's delete the first user:

```
user = User.first
user.delete
```

### Search for Specific Users

All the `find` methods work in Tux too!

```
user = User.find_by_id(2)
user = User.find_by(:name => "Beth")
user = User.first
user = User.last
```

Once you're done, just exit Tux by entering `exit`.

Clone this repo : <https://github.com/learn-co-curriculum/sinatra-activerecord-using-tux>
