Rails 对一个值之一建模,但对其他值的倍数建模
我有一个 Rails 应用程序,在附件模型中有一个名为 main_image 的字段,该字段是布尔值。它应该在数据库中只允许一个 true 但多个 false,但是由于我现在拥有范围,它只允许每个资产每个一个,这是另一个模型。如何允许每个附件有多个 false 但只有一个 true?
I have a rails application that has a field called main_image, which is boolean, in the attachment model. It is suppose to allow only one true but multiple false in the database, however as I have the scope now it only allows one of each for each asset, which is another model. How would allow multiple false but only one true per attachment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在已经通过在模型中使用以下代码解决了这个问题:
因为 :main_image 要么是 true 要么是 false,这表示仅当为 true 时才采用 main_image 字段并确保它对于每个 asset_id 都是唯一的,因为如果不是 true true 它将返回 false,从而跳过检查模型是否唯一。
I have now solved this issue by using the following code in my model:
since :main_image is either true or false this says to take the main_image field and make sure it is unique for each asset_id only if it is true, because if it is not true it will return false and thus skip checking if the model is unique.