Git Product home page Git Product logo

sinatra-activerecord-using-tux-london-whitehat-staff's Introduction

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

Oh no! We get an error that it doesn't recognize ActiveRecord. Let's try to require the ActiveRecord gem: require activerecord

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:

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 => "[email protected]", :fav_icecream => "mint chocolate chip")

Or:

user = User.new
user.name = "Beth"
user.email = "[email protected]"
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.

View Using Tux in Sinatra with ActiveRecord on Learn.co and start learning to code for free.

sinatra-activerecord-using-tux-london-whitehat-staff's People

Contributors

annjohn avatar devinburnette avatar dfenjves avatar drakeltheryuujin avatar maxwellbenton avatar pletcher avatar sophiedebenedetto avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.