views:

37

answers:

1

I'm building a Rails app that has Etsy.com style functionality. In other words, it's like a mall. There are many buyers and many sellers.

I'm torn about how to model the sellers. Key facts:

  • There won't be many sellers. Perhaps less than 20 sellers in total.
  • There will be many buyers. Hopefully many thousands :)
  • I already have a standard user model in place with account creation and roles.
  • I've created a 'role' of 'seller', which the admin will manually apply to the proper users. Since we'll have very few sellers, this is not an issue.

I'm considering two approaches:

(1) Create a 'store' model, which will contain all the relevant store information. Products would :belong_to :store, rather than belonging to the seller. The relationship between the user and store models would be: user :has_one store. My main problem with this is that I've always found has_one associations to be a little funky, and I usually try to avoid them. The app is fairly complex, and I'm worried about running into a cascade of problems connected to the has_one association as I get further along into development.

(2) Simply include the relevant 'store' information as part of the user model. But in this case, the store-related db columns would only apply to a very small percentage of users since very few users will also be sellers. I'm not sure if this is a valid concern or not.

It's very possible that I'm thinking about this incorrectly. I appreciate any thoughts.

Thanks.

+2  A: 

I would definitely use a relationship between a store and a user. This provides a lot more flexibility and is a much cleaner data design.

I have never had any issues using any of the basic associations in Rails/Active Record. What do you mean by "funky"?

Toby Hede
Thanks. I've started with this approach and seems like it makes sense. To be honest, I don't remember specifically why I thought the `has_one` association was weird. The problems occurred when I had just started working in Rails. So it could have just been general lack of familiarity.
MikeH