Review
Topics Covered: Databases and tables; Rails models; ActiveRecord; working with Rails models in rails console
.
Exercises
E1: ActiveReview
Again, we went over a lot of methods! Make sure you understand what each one returns, and whether they are a class or instance method. Look them up if you forgot.
-
.all
-
.first
-
.new
-
.find
-
.find_by
-
.where
-
.save
-
.update_attributes
-
.destroy
Q1: What's the difference between .find
and .find_by
?
Q2: What's the difference between .where
and .find_by
?
E2: ActiveQuiz
- What is a database?
- What is a database table? How does it relate to a database? What's a good analogy for a database table (i.e. a way to visualize it)?
- Draw the
tweets
database table
- Why do we use models/resources at all?
- What is ActiveRecord?
- What's the difference between a database table, resource and an ActiveRecord model?
E3: Console Fun
Fire up rails console
from within your blog rails app folder from yesterday. Then do the following things:
- Create a new Article.
- Find it with the
Article.find
method
- Find it by title with
Article.where(title: "...")
- Find it by title with
Article.find_by(title: "...")
- Describe to yourself what the difference is between those two methods.
- Update that Article to have a different title. Is this possible?
- Update that Article to have a different text. Is this possible?
- Update that Article to have a different id. Is this possible?
- Delete that Article.
iXtreme
A bit harder now:
- Create 10 Articles programmatically, each with a different title and different text.
- Destroy them all.
- Create 10 more the same way. Find the 4th one.
- Update all of them to have the text "iX TAs rock!"
- Update the fourth one to to have the text "And the teacher rocks sometimes, too".
E5 (Bonus!): Likey Likey
How would you find all Articles that have the word "awesome" in them? How would you find all Articles that have words like "awesome" in them (like meaning "awesum" or "awesick", not synonyms like "cool" or "great")?
Project
Lekker Plekke
Our cool class app Lekker Plekke will allow us to keep track of and share all of the cool places we find in Cape Town and South Africa.
So many places to go and so little time! Let's get started on our Lekker Plekke app:
Rails App
- Create a new rails app called lekker_plekke outside of your ixperience repository folder.
- Initialize a git repository and add all of your files to it, then commit it.
Places Model
- Generate a Place model. What attributes do you think Places would need? Try to think of at least two useful attributes (hint: name might be useful!).
- Use seeds.rb (or the rails console) to seed your database with a few cool places in Cape Town you've seen so far!
Places Controller
- Create a places controller, with the following methods: index and show
- Create appropriate views for index and show. On the index page, list all places by name. On the show page, display an individual place, including all attributes.
BONUS (Challenge):
- Also add create, new, edit, update and destroy methods.
- Add appropriate views for creating and editing places. (hint: forms may help)
- Add a link to the show page to edit and delete places using the correct action methods. Add a show link to each place on the index page.
Make your app look good!
P.S. This includes the bonus and extra extra extra bonus! If your doesn't exactly like this, don't panic. As long as the content for the index and the show pages are there, you're all set.
You can find example code in this Github Repo. Scout's honor: please only look at this code after you've already built your own version (including the bonuses).