Giter Site home page Giter Site logo

sinatra-activerecord-using-tux-v-000's Introduction

Using Tux in Sinatra with ActiveRecord

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.

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.

In order to do that, I need to open up IRB in terminal and require the file models/user.rb:

require models/user.rb

But then I get an error that it doesn't recognize ActiveRecord. But when you try to require the ActiveRecord gem, you get the following:

require activerecord

So suddenly, trying to test this method 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 => "Tricia", :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 = "tricia 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.

sinatra-activerecord-using-tux-v-000's People

Contributors

victhevenot avatar dfenjves avatar annjohn avatar pacosta613 avatar

Watchers

James Cloos 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.