DEV Community

Mary Webby
Mary Webby

Posted on

Ruby Database Setup & Common Commands & CRUD

Using PSQL in the rails database. Explaining set up and then going into common methods for accessing data within the database.

Note ➔ When editing and creating files within your app and outside of your terminal sql editor, you will need to start and restart the rails console to make sure the changes are incorporated.

➔ Commands to start your db for rails

  1. First, check out my other article on using SQL to create a table and the first object inside.

  2. Next, you will need to change your config/database.yml file to add your database you created. For example, database: my_contact_book.

  3. Next you will be adding your class file to the models folder, so for example, app/models/class.rb. In this folder will be, the base set up whats below. < ActiveRecord::Base allows Class to inherit all properties and methods in ActiveRecord. class Class < ActiveRecord::Base\nextLineend
    Image description

  4. You are also able to self.table_name = "contacts" and more properties to your class to manually enter here, instead of the command line.

  5. You will now need to open the terminal and type in irb, which will prompt the "interactive ruby", which is a tool to interactively execute Ruby expressions read from the standard input.

  6. Then you will require "./app/models/class" to ensure you have the file you need.

  7. Exit irb by executing exit, and type rails c or rails console, which will open the respective ruby PSQL.

➔ Methods to act on your class within the rails console.

ActiveRecord Relations are very similar to Arrays. Any method that you can call on an Array, you can also call on a Relation; .at, .each, .sample, etc.

  • Class.count returns an integer

  • Contact.all returns all the records in a table

  • x.at(0) x being and example for what to replace, and at(0) being where you are looking in the indexes.

  • c = Contact.all.sample taking out a random contact and storing it in the variable c.

  • c.id, c.created_at, c.first_name, c.phone, now we can call on that specific variable we created and access its contents.

  • Contact.all.order({ :last_name => :asc, :first_name => :asc, :date_of_birth => :desc }) retrieve info and sorting it in a specific way.

  • Contact.order(:first_name).reverse_order same thing with this one.

  • Contact.where({ :last_name => ["Betina", "Woods"] }) giving specific to try and find a name.

  • Contact.where({ :first_name => "Mickey" }).or(Contact.where({ :last_name => "Betina" })) Similar ask to before but instead you are broadening the search.

➔ CREATE READ UPDATE & DELETE

UPDATE

To update a row, first you need to locate it:

c = Contact.where({ :id => 2 }).first
Enter fullscreen mode Exit fullscreen mode

And then assign whatever new values you want to:

c.first_name = "Minerva"
Enter fullscreen mode Exit fullscreen mode

And then don’t forget to save the changes:

c.save
Enter fullscreen mode Exit fullscreen mode

DELETE

To delete a row, first find it:

c = Contact.where({ :id => 2 }).first
Enter fullscreen mode Exit fullscreen mode

And then,

c.destroy
Enter fullscreen mode Exit fullscreen mode

Intense.

➔ Creating tasks to populate helpful sample data

  1. Navigate to lib/tasks, and create file to create your task, such as lib/tasks/i_am_lazy.rake

  2. Place your task into the file,
    Image description or any task you want to create. This one is creating a new contact for a contact book.

  3. In our terminal we will call rake sample_contacts and if you look at your database it should be populated!

Top comments (1)

Collapse
 
kylestech95 profile image
Kyle A. • Edited

Hi Mary,

Your article is very informative & useful to learn from.
In the beginning of your article you mention starting & restarting the console for rails.

Currently, I am not familiar with Ruby/Rails language but for the server,would it be easier to utilize the nodemon npm
package to restart the server automatically?

stackoverflow.com/questions/361933...

In my article, dev.to/kylestech95/mongoosemongodb... , the option to run nodemon filename.js is included as well as npm run start.