简体   繁体   中英

How to model a user that can be a host, customer, or both? Rails

I am trying to rebuild something like airbnb as a side project and I'm wondering how to model a User entity that can be either a Host , a Customer , or both. How would one represent this?

The issue with polymorphism is that the User can only either a Host or a Customer but not both.

Is the best way to do this via two has-one relationships?

The way I would approach this problem is by having three models: User , Rental ,(home to rent), and RentalBooking (the booking a user makes)

A User has many Rental 's, and the Rental "host" would be determined by whoever owns the Rental (The rentals user_id). The RentalBooking would have a rental_id and a user_id (User ID being the customer).

Personally, I wouldn't necessary have a give the user a role of Host or Customer , but this information would easily be presentable in each booking or rental post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM