Methods and Products

Premium Content - Free Preview

User Methods

You saw how to set up code to create likes, now it's time to improve the code with some additional methods.


To make things simpler, create a method in User for liking products.

Model Code


Add another method to user that checks if a user likes a specific product. To do this, check if the user's liked_products includes a given product.
(To find a method that check if an item exists in a collection, see Ruby Arrays or look at Ruby's enumerable docs.)

Model Code


Can you add validations to the Like model to make sure every like has a user_id and product_id?

Model Code

dependent relationships

If you ever delete a user, their likes will still exist in the database.

End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Contact Us
Sign in or email us at [email protected]