[英]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. 我正在尝试将诸如airbnb之类的内容作为辅助项目进行重建,并且我想知道如何对可以是
Host
或Customer
或两者的User
实体进行建模。 How would one represent this? 一个人怎么表示呢?
The issue with polymorphism is that the User
can only either a Host
or a Customer
but not both. 多态性的问题在于,
User
只能是Host
或Customer
而不能同时是两者。
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) 我要解决此问题的方法是采用三种模型:
User
, Rental
,(房屋出租)和RentalBooking
(用户进行的预订)
A User
has many Rental
's, and the Rental
"host" would be determined by whoever owns the Rental
(The rentals user_id). 一个
User
有许多Rental
,并且Rental
“主机”将由拥有该Rental
(租赁user_id)确定。 The RentalBooking
would have a rental_id
and a user_id
(User ID being the customer). RentalBooking
将具有一个rental_id
和一个user_id
(用户ID为客户)。
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. 就个人而言,我不必让用户担任
Host
或Customer
的角色,但是此信息可以轻松地在每个预订或租赁帖子中显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.