开发者

Rails: Progressive Validation, use STI or something different?

开发者 https://www.devze.com 2023-03-24 19:30 出处:网络
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 a

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 开发者_如何学PythonCompletePhoto. 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.


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 of validates ..., :if => :complete.

0

精彩评论

暂无评论...
验证码 换一张
取 消