3

I'm working on a Rails 4 App where there is a model called Apps. Each App can have multiple certificates which are stored in the Certificates model. Out of the certificates each App can have a development and a production certificate which are then active for the current App. At a single given time only two certificates (development/production) can be active for the App. I'm trying to come up with an ActiveRecord structure for this logic but I'm lost. Any suggestions on this?

Apps (Has Many certificates)
  - development_certificate
  - production_certificate

Certificates (Belongs to App)
4

2 回答 2

3

更紧密地建模数据的解决方案只是两个一对一的关系:

class App < ActiveRecord::Base
  belongs_to :production_certificate, class_name: "Certificate"
  belongs_to :development_certificate, class_name: "Certificate"
end
于 2013-08-19T19:05:57.930 回答
1

好吧,在你的模型上放 2 个布尔值。一为development一为production。然后创建一个自定义验证器来检查创建,如果此应用程序有一个development或一个production证书处于活动状态。您还可以创建两个范围来更轻松地获取此证书。
你不需要想太多,或者以某种疯狂的方式对其进行建模。

于 2013-08-19T18:46:06.580 回答