For this assignment, we'll be working with an e-commerce domain. We'll be focusing on the product reviews.
We have three models: User
, Review
, and Product
.
For our purposes, a Product
has many User
s, a User
has many Products
s,
and a Review
belongs to a User
and to a Product
.
Product
- User
is a many to many relationship.
Note: You should draw your domain on paper or on a whiteboard before you start coding. Remember to identify a single source of truth for your data.
- Active Record Migrations
- Active Record Associations
- Class and Instance Methods
- Active Record Querying
To get started, run bundle install
while inside of this directory.
Build out all of the methods listed in the deliverables. The methods are listed in a suggested order, but you can feel free to tackle the ones you think are easiest. Be careful: some of the later methods rely on earlier ones.
Remember! This code challenge does not have tests. You cannot run rspec
and you cannot run learn
. You'll need to create your own sample instances so
that you can try out your code on your own. Make sure your associations and
methods work in the console before submitting.
We've provided you with a tool that you can use to test your code. To use it,
run rake console
from the command line. This will start a pry
session with
your classes defined. You can test out the methods that you write here. You are
also encouraged to use the seeds.rb
file to create sample data to test your
models and associations.
Writing error-free code is more important than completing all of the deliverables listed - prioritize writing methods that work over writing more methods that don't work. You should test your code in the console as you write.
Similarly, messy code that works is better than clean code that doesn't. First, prioritize getting things working. Then, if there is time at the end, refactor your code to adhere to best practices.
Before you submit! Save and run your code to verify that it works as you expect. If you have any methods that are not working yet, feel free to leave comments describing your progress.
The starter code has migrations and models for the initial User
and Product
models, and seed data for some User
s and Product
s. The schema currently
looks like this:
Column | Type |
---|---|
name | String |
Column | Type |
---|---|
name | String |
price | Integer |
You will need to create the migration for the reviews
table using the
attributes specified in the deliverables below.
Write the following methods in the classes in the files provided. Feel free to build out any helper methods if needed.
Deliverables use the notation #
for instance methods, and .
for class
methods.
Remember: Active Record give your classes access to a lot of methods already! Keep in mind what methods Active Record gives you access to on each of your classes when you're approaching the deliverables below.
Before working on the rest of the deliverables, you will need to create a
migration for the reviews
table.
- A
Review
belongs to aProduct
, and aReview
also belongs to aUser
. In your migration, create any columns yourreviews
table will need to establish these relationships. - The
reviews
table should also have:- A
star_rating
column that stores an integer. - A
comment
column that stores a string.
- A
After creating and running your migration, create your Review
class, and use
the seeds.rb
file to create Review
instances so you can test your code.
Once you've set up your reviews
table, work on building out the following
deliverables.
Use Active Record association macros and Active Record query methods where
appropriate (i.e. has_many
, has_many through
, and belongs_to
).
Review#user
- returns the
User
instance for this Review
- returns the
Review#product
- returns the
Product
instance for this Review
- returns the
Product#reviews
- returns a collection of all the Reviews for the Product
Product#users
- returns a collection of all the Users who reviewed the Product
User#reviews
- returns a collection of all the Reviews that the User has given
User#products
- returns a collection of all the Products that the User has reviewed
Use rake console
and check that these methods work before proceeding. For
example, you should be able to call User.first.products
and see a list of the
products for the first user in the database based on your seed data; and
Review.first.user
should return the user for the first review in the database.
Review#print_review
- should
puts
to the terminal a string formatted as follows:Review for {insert product name} by {insert user name}: {insert review star_rating}. {insert review comment}
- should
Product#leave_review(user, star_rating, comment)
- takes a
User
(an instance of theUser
class), astar_rating
(integer), and acomment
(string) as arguments, and creates a newReview
in the database associated with this Product and the User
- takes a
Product#print_all_reviews
- should
puts
to the terminal a string representing each review for this product - each review should be formatted as follows:
Review for {insert product name} by {insert user name}: {insert review star_rating}. {insert review comment}
- should
Product#average_rating
- returns a
float
representing the average star rating for all reviews for this product
- returns a
User#favorite_product
- returns the product instance that has the highest star rating from this user
User#remove_reviews(product)
- takes a
Product
(an instance of theProduct
class) and removes all of this user's reviews for that product - you will have to delete any rows from the
reviews
table associated with this user and the product
- takes a