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
-
First, check out my other article on using SQL to create a table and the first object inside.
Next, you will need to change your
config/database.yml
file to add your database you created. For example,database: my_contact_book
.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
allowsClass
to inherit all properties and methods in ActiveRecord.class Class < ActiveRecord::Base
\nextLineend
You are also able to
self.table_name = "contacts"
and more properties to your class to manually enter here, instead of the command line.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.Then you will
require "./app/models/class"
to ensure you have the file you need.Exit
irb
by executingexit
, and typerails c
orrails 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 integerContact.all
returns all the records in a tablex.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
And then assign whatever new values you want to:
c.first_name = "Minerva"
And then don’t forget to save the changes:
c.save
DELETE
To delete a row, first find it:
c = Contact.where({ :id => 2 }).first
And then,
c.destroy
Intense.
➔ Creating tasks to populate helpful sample data
Navigate to
lib/tasks
, and create file to create your task, such aslib/tasks/i_am_lazy.rake
Place your task into the file,
or any task you want to create. This one is creating a new contact for a contact book.In our terminal we will call
rake sample_contacts
and if you look at your database it should be populated!
Top comments (1)
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 asnpm run start
.