CSC 486: Ruby on Rails – Rails Command Line

TL: DR

Overview:

Now, I going to explain the Rails Command Line. The Rails allows me the programme mainly the power to enter data into the database without being within mySQLite which is pretty awesome.

Here is how it works:

First once in the database. Open up your project. Once inside type the follow in the terminal.

rails c

Note: I am assuming that whoever is seeing this already has a working build and already are models made. If not please see my post on our build. If you don’t know how to make a model, try  Rails for Zombies.

Once the command line is up and running it is where the cool stuff beginnings.Let start with making a data entry.

Type: rails c like seen below:

Untitled4.png

This will generate a question with the field set nil unless you set them differently in your model. To change the value of one of the tags in new question (q), you need to set the a value to the q with tags in mind, like so; q.title = “Test”

Untitled7.png

As you see above we input the command and Rails tell us that the command passed. Let see it again but this time with the body tag. After setting the body tags, we need to save the data to the database if we want to use it. This command is very easy, it is q.save.

Untitled9.png

As you again rails passed the data to the database and we see again an example of how to set a value to tag in the database. One thing I found tricky at first was how to exit the command line; this is done by just typing exit. As you see below.

Untitled10.png

Now, let’s try to view the data from the command line. You will do this by repeating the same first step but with a twisted. The twist is that this time we are asking the database something. Let ask Rails where it see the title “Test” in the database.

Untitled11.png

After looking you see that rails prints out every record that has a title with “Test”. This is a great to find data without going into an another application. Another this we can do is ask Rails to find something for us. In this case, we are asking if Rails can find us a record within the question that has an id of 4. We found that the previous data we made is the fourth data in the table which means that it has an id of 4.

Untitled12.png

After, having Rails search a total of 0.5ms we found our data. Which, now rises that through now that we have the data stored in q can we edit as well. The answer is Yes, you can and it the same way you set a value to a tag when you were creating the data. You even run the same command for saving the data to database and it will tell you how you are updating the data in the database.

Untitled15.png

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s