Rails:渐进式验证,使用 STI 还是不同的东西?
我有一个 Rails 应用程序,用户可以在其中共享特定类型的照片。目前,该应用程序要求照片以多种方式分类才能生效,因此用户必须一次上传一张照片并对它们进行分类才能将其保存到数据库中。
分类需要一些时间,所以我希望允许用户上传一批照片,然后有时间再回来分类,但是当照片存储时没有完全分类时,我不希望它们与“完整”混在一起”的照片。
理想情况下,我希望这是一种“向导”系统,用户可以一次上传一堆照片,然后在有时间时继续浏览他们的个人队列并对每张照片进行分类(以完成创建)。
我的问题是:你会如何解决这样的问题?
我一直在考虑使用单表继承来创建 Photo 的两个子类:IncompletePhoto
和 CompletePhoto
。 IncompletePhoto
仅需要图像文件本身,但 CompletePhoto
需要分类。用户可以查看自己的不完整照片,但应用程序内的搜索结果只会返回完整照片。
这听起来像是解决我试图解决的问题的正确方法吗?还是有更好的方法?我以前从未使用过性传播感染,我不确定这是否是一个好主意。
I have a rails app where users share specific kinds of photos. Currently the app requires photos to be categorized in several ways before they are valid, hence users must upload photos one at a time and categorize them in order to save them to the database.
Categorization takes some time, so I'd like to allow users to upload batches of photos and then come back and categorize them when they have time, but when photos are stored without being fully categorized I don't want them mixed in with "complete" photos.
I'd ideally like this to be a sort of "Wizard" system where users can upload a bunch of photos at once and then proceed through their personal queue and categorize each photo (to finish creating it) when they have time.
My question is: how would you approach a problem like this?
I've been thinking about using Single Table Inheritance to create two subclasses of Photo: IncompletePhoto
and CompletePhoto
. The IncompletePhoto
would only require the image file itself, but CompletePhoto
would require categorization. Users could view their own IncompletePhotos, but search results within the app would only return CompletePhotos.
Does that sound like the right approach for the problem I'm trying to solve, or is there a better way? I've never used STI before and I'm not sure whether or not it's a good idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说,当您拥有不同的对象,这些对象具有一些但不是全部通用属性时,STI 的创建非常有用,在这种情况下,您可以从 DRY 中受益数据库和模型。我不确定是否有办法正确更改此类模型的实例类型。好吧,您可以只修改
type
列本身,但对象的 Ruby 类将是相同的,验证也是如此,除非您在保存后重新获取模型,然后运行验证手动。后者对我来说听起来像是一个肮脏的黑客。作为正确的方法,我建议您添加
complete
列,并使用validates ..., :if =>; 形式的验证器。 :完成
。I'd say that STI was created to be useful when you have different objects with some, but not all common properties, for the cases where you'll benefit from DRY in both database and models. I'm not sure if there is a way to correctly change the type of instance of such a model. Well, you can just modify the
type
column itself, but the Ruby class of the object will be the same, and so will be validations, unless you will re-fetch the model after saving and then run validations manually. The latter sounds like a dirty hack for me.As a correct way, I'd suggest you to add
complete
column, and use validators in form ofvalidates ..., :if => :complete
.