# Dynamic models

**URL:** <https://www.ruby-forum.com/t/dynamic-models/137154>\
**Category:** Rails\
**Created:** [May 9, 2008, 2:12pm UTC](https://www.ruby-forum.com/t/dynamic-models/137154 "2008-05-09T14:12:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 9, 2008, 2:12pm UTC](https://www.ruby-forum.com/t/dynamic-models/137154/1 "2008-05-09T14:12:26Z")

</div>

Hi

In my app , a user can create a set of question then a table is  
dynamily created , in the database,to store the answers of the  
questions. the name of the table is answers\_x (x is the id of the  
questionniare).

Is it possible to dynamicly create a model, to handle the answer\_x  
tables ?? I can’t manually create a model then restart the server.

---

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 9, 2008, 2:34pm UTC](https://www.ruby-forum.com/t/dynamic-models/137154/2 "2008-05-09T14:34:18Z")

</div>

jef wrote:

> Hi
> 
> In my app , a user can create a set of question then a table is  
> dynamily created , in the database,to store the answers of the  
> questions. the name of the table is answers\_x (x is the id of the  
> questionniare).
> 
> Is it possible to dynamicly create a model, to handle the answer\_x  
> tables ?? I can’t manually create a model then restart the server.

Why a different table per set of questions?

Sounds like what you need is actually are more generic models (Quizzes  
and Quizitems?) that stores lists of questions and answers.

Assuming you have a User model defined somewhere else…

User  
has\_many :quizzes

Quiz  
belongs\_to :user  
has\_many :quizitems

Quizitem  
belongs\_to :quiz

where a quizitem contains a question and an answer… maybe points if  
you’re scoring them somehow.

---

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 9, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/dynamic-models/137154/3 "2008-05-09T15:06:33Z")

</div>

> where a quizitem contains a question and an answer… maybe points if  
> you’re scoring them somehow.

a question is asked to a lot of people. For a quizitem there are many  
answers.

We don’t know by advance the type of the answer. It may be an integer,  
(what is the age of the captain) or a string (what is the name of the  
captain).

It’s up to the end user. He defines the quizz. and other people answer  
to that quizz.

---

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 9, 2008, 4:06pm UTC](https://www.ruby-forum.com/t/dynamic-models/137154/4 "2008-05-09T16:06:09Z")

</div>

jef wrote:

> > where a quizitem contains a question and an answer… maybe points if  
> > you’re scoring them somehow.
> 
> a question is asked to a lot of people. For a quizitem there are many  
> answers.
> 
> We don’t know by advance the type of the answer. It may be an integer,  
> (what is the age of the captain) or a string (what is the name of the  
> captain).
> 
> It’s up to the end user. He defines the quizz. and other people answer  
> to that quizz.  
> Hmm… then the end user can define the expected type of the response:  
> number, text, date, time, whatever.

User  
has\_many :questions

Question  
belongs\_to :user  
has\_many :answers

# question has a prompt, also has a ‘type’ for the answer. You can

store all the answers as string, and just run a validation of an answer  
according to the expected ‘type’ for the response. if the expected type  
is a number, take the answer text and see if it can convert cleanly to a  
number. Perhaps a polymorphic Answer where you can have a numeric  
answer, a text answer, etc.

Answer  
belongs\_to :question

# answer has text in it

---

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 9, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/dynamic-models/137154/5 "2008-05-09T18:43:15Z")

</div>

If you really do want to do this, you can do it with the eval method.

eval will execute a string as ruby code.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #4 parts a and b now available!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 13, 2008, 10:48am UTC](https://www.ruby-forum.com/t/dynamic-models/137154/6 "2008-05-13T10:48:16Z")

</div>

Generating a new model for each question is not the right way to do  
this. I feel you are making things way too hard. There is no reason you  
can’t just use a solution similar to what Ar Chron explained. In short,  
create a Question and Answer model. Just store the answers as string and  
the questions as strings and then store the type of the question /  
answer as well and simply cast the answer to the type. or go the  
polymorphic route and have an answer model for each possible answer  
type. Bottom line, creating arbitrary dynamic models for each question  
is simply not the path to take.

---

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 13, 2008, 10:27am UTC](https://www.ruby-forum.com/t/dynamic-models/137154/7 "2008-05-13T10:27:53Z")

</div>

can you give me an example julian

I know that eval(‘puts “foo”’) print the the foo string. But I don’t  
know how to generate a new model.  
Thank you.
