Skip to content

Instantly share code, notes, and snippets.

@julsfelic
Last active February 17, 2016 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julsfelic/70a1d970c2bfbd63588f to your computer and use it in GitHub Desktop.
Save julsfelic/70a1d970c2bfbd63588f to your computer and use it in GitHub Desktop.
"Migrations, Databases, Models, and Relationships in Rails

One-to-one Relationship Example

A person belongs to one Fitbit and a Fitbit belongs to one person.

One-to-many Relationship Example

A article of clothing belongs to a person and a person has many articles of clothing.

Many-to-many Relationship

A movie could have many genres and a genre could have many movies. The table name would be movie_genres.

What's the difference between test, development and production databases?

  • When writing tests we may want to clean the database after each test so we have a dedicated database called test just for that.
  • We use the development database when we want to get an a real world experience of how our app functions without messing around with real user data.
  • The production database is used in the real world that contains real user data that should not be tampered with.

How do you create a Rails app from the command line with a postgres database?

$ rails new appname -d postgresql

What files are created by typing rails g model...

  • A model file
  • A migration file
  • A model test file
  • A text fixture file

What is the difference between typing rails g model... and rails g migration...

rails g model creates all associated files (test files, model and migration) while rails g migration just creates the migratio file (w/o timestamps).

#Imagine that the items table has a category called quantity. What command would you type if you wanted to get rid of the quantity attribute?

$ rails g migration RemoveQuantityFromItems quantity:integer

Imagine that you have a table students. What is the ActiveRecord query that would return all students with the first name of Richard?

Student.where(first_name: "Richard")

How would you update the student record with ID 4 to have a new phone number of "101-222-3333"?

  Student.find(4).update(phone_number: "101-222-3333")
  Student.update(4, phone_number: "101-222-3333")
  student = Student.find(4)
  
  student.phone_number = "101-222-3333"
  student.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment