Take 30 minutes and answer the following questions together. Use a text editor or whiteboard to sketch out your SQL.
Looking at Documentation is an important part of programming. You don't have to memorize anything, but you should get familiar with the types of information you can find in different docs. For this exercise, go through the ActiveRecord documentation here with your partner and answer the following questions about each method.
- What argument or arguments does the method take?
- What type of object does the method return?
- What happens if none of the parameters match? (i.e. what if
Tweet.find(5)
can't find that tweet? How aboutTweet.find_by(id: 6)
?
Methods:
find
.find_by
.where
.all
.first
.destroy
Pretend that you have a tweets
table with two columns - message
and user_id
. Given the code below, what SQL statements will fire when the following methods are called?
class Tweet < ActiveRecord::Base
end
Tweet.all
Tweet.find(5)
Tweet.find_by(user_id: 7)
Tweet.where(user_id: 7)
Tweet.create(user_id: 5, message: 'making some coffee')
Tweet.destroy(7)