我试图建立一个小型的应用程序来销售门票与PayPal。我们有两种门票,标准票和贵宾票。
我想要实现的是,当用户访问事件/显示页面,点击购买门票按钮,然后他被定向到一个付款页面,选择他想要购买的门票类型,然后结账。
我对如何设置模型之间的关联感到困惑。
这是我到目前为止所拥有的
class Event < ApplicationRecord
has_many :tickets
end
class Payment < ApplicationRecord
belongs_to :ticket
end
class User < ApplicationRecord
end
class Ticket < ApplicationRecord
enum type: [ :standard, :vip ]
belongs_to :event
end
在事件、票证和TicketType之间创建关系的最佳选项是什么,以及应在其数据库中存在哪些引用
发布于 2018-08-20 03:33:57
你在正确的轨道上-你需要的只是用户和票证之间的关联,以及将它们联系在一起的间接关联。
class Event < ApplicationRecord
has_many :tickets
has_many :users, through: :tickets
end
class Payment < ApplicationRecord
belongs_to :ticket
has_one :event, through: :ticket
has_one :user, through: :ticket
end
class User < ApplicationRecord
has_many :tickets
has_many :payments, through: :tickets
has_many :events, through: :tickets
end
class Ticket < ApplicationRecord
enum level: [ :standard, :vip ]
belongs_to :event
belongs_to :user
has_many :payments
end
在票证和支付之间使用has_many
关联是一个很好的选择,因为它可以让您处理跟踪失败的支付。
但是您应该注意在ActiveRecord中为枚举或任何其他类型的列使用名称type
,因为它具有特殊的意义,因为它用于推断要在多态关联或STI中加载的类-它可能会产生意想不到的后果。
https://stackoverflow.com/questions/51911479
复制相似问题