Reputation: 778
I have a User model. I am working in an e-commerce marketplace application. I need to be able to switch User from buyer to seller. If the user wants to be seller then there exists another table called sellerinfo to fill in his seller information. I have a column in Users table called is_seller? . So if the user is seller then I must create an association so I can put more information about him. Is it ok to do like this from user.rb file:
has_one :seller_info if user.is_seller?
I think this is not Rails way. Please suggest how to switch users and store their data.
Upvotes: 0
Views: 722
Reputation: 1172
You should have at least three tables:
1 - user table, here go just the credentials
2 - user buyer table (has_one association), where go user's personal information
2.1 - address user buyer (has_many or two has_one association from user buyer table) if you want let the user have more than one delivery address.
3 - user seller table, where go sellers information
For the user's seller table:
Use has_one association with Optional set to True for the table User's sellers (it will permitted have null values for this association). Also consider creating the has_one association only if the users fill out all the pre requirements. Therefore will exists only the users who wants to sell in User's seller table. To see with the user is a seller just check with the has_one existed or not.
About the flag user_seller:
You can use the flag user_is_seller created in user table to track the state of the user, if he is in mode of seller or not. If yes, than you can have controllers just for that state (use before_filters to filter the user's who is not a seller or is not in seller mode).
That way, you can have existing users who is sellers and buyer in the same account.
Upvotes: 1
Reputation: 1451
Using the code like this, it allows you to use all ActiveRecord association's benefits
Also, during the create/update you have to consider:
seller_info
, if user moves to buyer type.is_seller?
valueUpvotes: 1