# How do you allow users make custom shopping lists?

**URL:** <https://www.ruby-forum.com/t/how-do-you-allow-users-make-custom-shopping-lists/184252>\
**Category:** Rails\
**Created:** [February 27, 2010, 7:22am UTC](https://www.ruby-forum.com/t/how-do-you-allow-users-make-custom-shopping-lists/184252 "2010-02-27T07:22:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_programmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@The\_programmer](https://www.ruby-forum.com/u/The_programmer)\
**Post date:** [February 27, 2010, 7:22am UTC](https://www.ruby-forum.com/t/how-do-you-allow-users-make-custom-shopping-lists/184252/1 "2010-02-27T07:22:33Z")

</div>

Hi,  
I asked Ryan bates, the owner of [railscasts.com](http://railscasts.com) if he knew of any  
documentation that would explain what code to use to allow users make  
their own lists in RoR. He referred me to here. I’m trying to make it  
so user A can make a shopping list and populate it with items like  
eggs, fruit, oatmeal, etc. I know that I could easily make one  
shopping list for all users to use but my situation is different. I  
want user A to be able to make a list for different types of food and  
such. The problem is, I don’t know if this is possible or how to do it  
without other users seeing their shopping list. I’m very adamant on  
letting users make their own shopping lists so any info on how to do  
that is very much appreciated.

---

<div class="post-metadata">

**Author:** ![The\_programmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@The\_programmer](https://www.ruby-forum.com/u/The_programmer)\
**Post date:** [February 27, 2010, 9:37am UTC](https://www.ruby-forum.com/t/how-do-you-allow-users-make-custom-shopping-lists/184252/2 "2010-02-27T09:37:44Z")

</div>

I’d start by creating various models to represent the user, the lists,  
the items _in_ the lists, and a pairing relationship between them (aka  
a “join table”, or in Rails lingo, a “has many through”  
relationship). For example, you could have 4 models:

User  
ShoppingList  
Food  
Foodable (the join model)

User has all the standard fields you need for a user with  
authentication, etc. To handle authentication, do a Google search for  
“authlogic” and do some reading on it.

ShoppingList consists only of:

user\_id  
name

(You could also add “notes” or something to that effect here too, if  
you wanted.)

Finally, Food would simply be:

name

…that’s it. So now you have:

## User:

id  
username  
email  
…etc…

## ShoppingList

id  
user\_id  
name

## Food

id  
name

## Foodable (the join model between foods and shopping lists)

id  
shopping\_list\_id  
food\_id

So you can see how, using foreign keys, we can link any number of  
foods to multiple shopping lists without duplicating information in  
the database. For example, if we had a food called “T-Bone Steak”, it  
only needs to be listed in the database ONCE. Not hundreds of times.  
Then, in the Foodable join table, we link its unique ID in the food  
table (say that ID is 199) to every list in which it appears.

By linking the shopping list model directly to the user model (so  
shopping\_list belongs\_to :user), we ensure that any single shopping  
list belongs to ONLY one user at any given time, meaning that your  
database select query (or a “find” on that model) will only return the  
lists that specific user owns, never anyone else’s, making it all but  
impossible that user A would see user B’s list.

This is basically a data architecture/database normalization problem.  
I suggest looking up the basics of how ActiveRecord works, especially  
model relationships such as belongs\_to and has\_many with the :through  
option (you could also do a has\_and\_belongs\_to\_many, but I hear that  
practice is falling out of favor these days because the  
has\_many :through allows you to work directly with the join model and  
does the same thing.)

You were on the right track checking out Railscasts - Ryan does a  
great job of breaking down complex topics to make them easy to  
understand. I suggest you look through it a bit more to learn more  
about how ActiveRecord works. In addition, check out Rails Guides:

> **[Getting Started with Rails — Ruby on Rails Guides](https://guides.rubyonrails.org/getting_started.html)**
>
> Getting Started with RailsThis guide covers getting up and running with Ruby on Rails.After reading this guide, you will know: How to install Rails, create a new Rails application, and connect your application to a database. The general layout of a...

- Will help make sure you have the foundational knowledge you need to  
get started  
[Active Record Associations — Ruby on Rails Guides](http://guides.rubyonrails.org/association_basics.html)
- Explains ActiveRecord associations like has\_many, belongs\_to,  
has\_many :through, etc.

Good luck.
