Shpigford
Shpigford

Reputation: 25378

How to create a "template" of records in a Rails app?

Users can create surveys. Survey's have questions and questions have answer options (ie. a multiple choice question).

What I want to do is create survey templates...so users can, with the click of a button, create a new survey with pre-set questions and answer options.

We've already got a functioning app, so that's not the question. And we've already got the ability to duplicate/copy/clone surveys as well. Also not the question.

The question is, how do I create a bunch of predefined surveys/questions/answer options that any user of the app has access to?

Do I store all the settings as a hash in some sort of templates table (and then just run Survey.create({:questions => {:answers =>{}})? Or some other method that I'm just not thinking of?

Does that make sense? Any more clarification needed?

Upvotes: 3

Views: 306

Answers (2)

Kris
Kris

Reputation: 2128

Why don't you add a flag (boolean) called is_template to the survey model? Then you can just create a normal survey and set the is_template flag high. Users can then see all surveys with this flag set and copy them.

Upvotes: 2

Tilo
Tilo

Reputation: 33742

You could use Single Table Inheritance for the Survey model, to distinguish between Surveys and SurveyTemplates. This way you can also better integrate Authorization.

When using STI in Rails, the model needs a :type attribute, and Rails stores the class name in that attribute for each record.

See:

http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html

Upvotes: 3

Related Questions