Forum: Ruby on Rails Column with dynamic db type

E46593732261d73ee99adc380b7f767a?d=identicon&s=25 Greg Ma (gregorylepacha)
on 2012-05-02 22:25
Hi,

I have a Question model where the answer to the question can have
multiple db type (integer, string, boolean, ...).

I would like to know if you have a better solution than this one:

Question:
question:string
answer_type:string

AnsweredQuestion:
question_id:integer
answer:text
answer_type:string

Ex: AnsweredQuestion.create(:question => 1, :answer => '5', :answer_type
=> 'Integer')

My problem with this solution is that I can't question is that I can't
question the answered questions where the answer is > 2.

Any better idea?
955103d80e386882143a78875fb727bf?d=identicon&s=25 Jeremy Walker (Guest)
on 2012-05-02 23:42
(Received via mailing list)
On 2 May 2012, at 21:25, Greg Ma <lists@ruby-forum.com> wrote:

> Hi,
>
> I have a Question model where the answer to the question can have
> multiple db type (integer, string, boolean, ...).

I've had a similar situation. I'd use STI to have different models for
different answer types. My repository at
http://github.com/ihid/super_sti might help. Email me personally if you
want some help with that repository.

Jeremy Walker
http://www.ihid.co.uk
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2012-05-03 00:16
Jeremy Walker wrote in post #1059314:
> On 2 May 2012, at 21:25, Greg Ma <lists@ruby-forum.com> wrote:
>
>> Hi,
>>
>> I have a Question model where the answer to the question can have
>> multiple db type (integer, string, boolean, ...).
>
> I've had a similar situation. I'd use STI to have different models for
> different answer types. My repository at
> http://github.com/ihid/super_sti might help. Email me personally if you
> want some help with that repository.

Here's a direct and elegant solution: http://www.mongodb.org/
If rigid schema is the problem, then eliminate your dependence on a
pre-defined schema.

However, the STI solution mentioned above is probably the next best
approach.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.