简体   繁体   中英

Modeling design question (Ruby On Rails)

I am converting a php project into ruby on rails. The previous project used mysql as the database and the rails project will do the same.

In the project there is a concept of a "step", which a course can have many of. A step has 1 of 5 types. (Quiz, Video, Text..etc) Obviously the type of data required for each step is different.

The way it was modeled before is to store all steps in the same table and have a field that holds a serialized php object of the step. I think this is bad for a couple reasons...You can' really query against serialized data, and it makes adding data or changing the structure of an object very hard.

Would it make more sense to model this data in a database instead of objects?

Generally yes. Rails has two possible ways to model this:

  1. Single Table Inheritance - Several models inheriting from a common ancestor, stored in the same table. Good if your steps are really similar to each other. Here you would call has_many on the parent model.

  2. Polymorphic associations - the association macros have an option :polymorphic which allows you to have different models in one association (each can have it's own table). I'd go for that if your steps have little in common.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM