Topics Covered: Coding databases and tables; Rails models; ActiveRecord; working with Rails models in rails console
.
Occasionally (usually once/week), we'll have you guys do a short checkpoint questionnaire so we can gauge how we're teaching the subject matter, and you yourself can gauge how well you're internalizing the concepts. They're designed to be a bit challenging, so please be careful in your answers.
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—thankfully, Rails has really good documentation with APIDock.
.all
.first
.new
.find
.find_by
.where
.save
.update_attributes
.destroy
What's the difference between .find
and .find_by
?
What's the difference between .where
and .find_by
?
The first set of methods will be left for you to look up.
find
takes one parameter, an integer, which is the ID of the resource. e.g. Tweet.find(1)
gets the tweet with ID 1. find_by
takes a hash, that has the parameter and a value, something like Tweet.find_by(:handle => "fizzcan")
.
.find_by
returns just the first matching entry, whereas .where returns all matching entries.
Fire up rails console
from within your 'twitter' rails app folder from class today. Then do the following things. After you're confident in your code, write the commands you wrote for each step in d7/activerecord_commands
:
User
with a name and followers countUser.find
. Assign the result to the variable user
User.find_by
followers
count equal to 10. user
from step 2 to have a different name. Make sure to save!Tweet
for this user. Assign the correct user_id
, and make sure to save. A bit harder now: