possible to have model in Mongoid/Rails with a field that varies in number? The Next CEO of Stack OverflowDynamic attributes with Rails and Mongoidcan't get mongoid/rails field to accept input as arrayis “collection” a reserved word in rails 3.2, simple_form or in mongoid/mongodb?Rails Nested Forms and Model Inheritance“Large data” work flows using pandasMongoDB/Mongoid: is it possible to have an index on multiple fields with both unique and dropDups options?Mongoid Scope Check If Array Field Contains ValuePreventing Mongoid 4.0.0 model field coercion of id => _idMongoid + rails 4 - implementing User & Following: uniquness validation of embedded modelNamespace models with mongoid

Multi tool use
Multi tool use

Is there always a complete, orthogonal set of unitary matrices?

Does Germany produce more waste than the US?

Running a General Election and the European Elections together

A small doubt about the dominated convergence theorem

Would a completely good Muggle be able to use a wand?

Is it convenient to ask the journal's editor for two additional days to complete a review?

Why is my new battery behaving weirdly?

Make solar eclipses exceedingly rare, but still have new moons

How to prove a simple equation?

How to edit “Name” property in GCI output?

How to get from Geneva Airport to Metabief, Doubs, France by public transport?

Do I need to write [sic] when a number is less than 10 but isn't written out?

Is it professional to write unrelated content in an almost-empty email?

Calculator final project in Python

Where does this common spurious transmission come from? Is there a quality difference?

Math-accent symbol over parentheses enclosing accented symbol (amsmath)

Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?

How is this set of matrices closed under multiplication?

How do I align (1) and (2)?

Can we say or write : "No, it'sn't"?

The past simple of "gaslight" – "gaslighted" or "gaslit"?

Axiom Schema vs Axiom

Why do airplanes bank sharply to the right after air-to-air refueling?

Is wanting to ask what to write an indication that you need to change your story?



possible to have model in Mongoid/Rails with a field that varies in number?



The Next CEO of Stack OverflowDynamic attributes with Rails and Mongoidcan't get mongoid/rails field to accept input as arrayis “collection” a reserved word in rails 3.2, simple_form or in mongoid/mongodb?Rails Nested Forms and Model Inheritance“Large data” work flows using pandasMongoDB/Mongoid: is it possible to have an index on multiple fields with both unique and dropDups options?Mongoid Scope Check If Array Field Contains ValuePreventing Mongoid 4.0.0 model field coercion of id => _idMongoid + rails 4 - implementing User & Following: uniquness validation of embedded modelNamespace models with mongoid










0















I'm trying to create a questionnaire app in Rails using Mongoid. I keep stumbling over the database setup because I'm new to things.



I want the users to be able to create questions with the possibility for varying numbers of answers. Some questions might have two possibilities: true, false. Some might have four or five possibilities.



So I've tried to create a question model and an answer model, then embed the answers in the question. I've tried a model with question:string answer-a:string answer-b:string answer-c:string and so on. But both approaches seem stupid and unwieldy.



Is there a way to create a model that allows someone to create a question field and an answer field, but such that the answer field can have multiples? So, create question, add an answer, and keep adding answers until they've finished their multiple choice?










share|improve this question




























    0















    I'm trying to create a questionnaire app in Rails using Mongoid. I keep stumbling over the database setup because I'm new to things.



    I want the users to be able to create questions with the possibility for varying numbers of answers. Some questions might have two possibilities: true, false. Some might have four or five possibilities.



    So I've tried to create a question model and an answer model, then embed the answers in the question. I've tried a model with question:string answer-a:string answer-b:string answer-c:string and so on. But both approaches seem stupid and unwieldy.



    Is there a way to create a model that allows someone to create a question field and an answer field, but such that the answer field can have multiples? So, create question, add an answer, and keep adding answers until they've finished their multiple choice?










    share|improve this question


























      0












      0








      0








      I'm trying to create a questionnaire app in Rails using Mongoid. I keep stumbling over the database setup because I'm new to things.



      I want the users to be able to create questions with the possibility for varying numbers of answers. Some questions might have two possibilities: true, false. Some might have four or five possibilities.



      So I've tried to create a question model and an answer model, then embed the answers in the question. I've tried a model with question:string answer-a:string answer-b:string answer-c:string and so on. But both approaches seem stupid and unwieldy.



      Is there a way to create a model that allows someone to create a question field and an answer field, but such that the answer field can have multiples? So, create question, add an answer, and keep adding answers until they've finished their multiple choice?










      share|improve this question
















      I'm trying to create a questionnaire app in Rails using Mongoid. I keep stumbling over the database setup because I'm new to things.



      I want the users to be able to create questions with the possibility for varying numbers of answers. Some questions might have two possibilities: true, false. Some might have four or five possibilities.



      So I've tried to create a question model and an answer model, then embed the answers in the question. I've tried a model with question:string answer-a:string answer-b:string answer-c:string and so on. But both approaches seem stupid and unwieldy.



      Is there a way to create a model that allows someone to create a question field and an answer field, but such that the answer field can have multiples? So, create question, add an answer, and keep adding answers until they've finished their multiple choice?







      ruby-on-rails mongodb mongoid






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 9 '12 at 4:55









      mu is too short

      354k58701676




      354k58701676










      asked May 9 '12 at 4:00









      charliemageecharliemagee

      503417




      503417






















          2 Answers
          2






          active

          oldest

          votes


















          2














          If answers are just strings then you could use an array field:



          class Question
          include Mongoid::Document
          #...
          field :answers, :type => Array
          end


          If answers have some internal structure (perhaps you want to track when they were created or changed or whatever), then you could use embeds_many and two models:



          class Question
          include Mongoid::Document
          #...
          embeds_many :answers
          end

          class Answer
          include Mongoid::Document
          #...
          embedded_in :question
          end


          Either one will let you work with q.answers in a natural list-like way so rendering such things is a simple matter of <% q.answers.each do |a| %> and you could shuffle the answers to display them in a random order.






          share|improve this answer

























          • Thanks mu. I already made the :type => Array attempt, but what I'm struggling with is the form. I still only get one field for answers. I guess I was hoping that the scaffold would automagically create a little + plus sign next to the answer field. Click that and you get another answer field, and so on until you've got all your answer choices.

            – charliemagee
            May 9 '12 at 5:14






          • 1





            @charliemagee: AFAIK you have to do that yourself, it should only take a couple lines of jQuery, just a quick .clone and .append.

            – mu is too short
            May 9 '12 at 5:36











          • Thanks mu, I'll try that out.

            – charliemagee
            May 9 '12 at 17:41











          • I got the cloning of fields bit to work with $("#plusanswer").prev().clone().insertBefore("#plusanswer"); but I get an error that it's expecting an array not a string. And my last field is the only one it takes. The others are getting ignored.

            – charliemagee
            May 11 '12 at 2:11












          • The server is complaining about a string where it expects an array?

            – mu is too short
            May 11 '12 at 2:45


















          0














          If you want dynamically generated forms for nested models, I suggest following this RailsCast: http://railscasts.com/episodes/196-nested-model-form-part-1



          The RailsCast method uses application helpers to dynamically generate the new objects.



          I tend to prefer @mu's method of using jQuery to create form elements. The nice thing about Rails is that, when passing nested attributes, you can give it whatever index you want. And so I'll generate a new form with an index of, say, Time.now.to_s and no ID parameter. And so my controller receives a parameter that looks like this:



          "question" => "answers_attributes" => "1234567" => "description" => "New answer" 





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10509632%2fpossible-to-have-model-in-mongoid-rails-with-a-field-that-varies-in-number%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            If answers are just strings then you could use an array field:



            class Question
            include Mongoid::Document
            #...
            field :answers, :type => Array
            end


            If answers have some internal structure (perhaps you want to track when they were created or changed or whatever), then you could use embeds_many and two models:



            class Question
            include Mongoid::Document
            #...
            embeds_many :answers
            end

            class Answer
            include Mongoid::Document
            #...
            embedded_in :question
            end


            Either one will let you work with q.answers in a natural list-like way so rendering such things is a simple matter of <% q.answers.each do |a| %> and you could shuffle the answers to display them in a random order.






            share|improve this answer

























            • Thanks mu. I already made the :type => Array attempt, but what I'm struggling with is the form. I still only get one field for answers. I guess I was hoping that the scaffold would automagically create a little + plus sign next to the answer field. Click that and you get another answer field, and so on until you've got all your answer choices.

              – charliemagee
              May 9 '12 at 5:14






            • 1





              @charliemagee: AFAIK you have to do that yourself, it should only take a couple lines of jQuery, just a quick .clone and .append.

              – mu is too short
              May 9 '12 at 5:36











            • Thanks mu, I'll try that out.

              – charliemagee
              May 9 '12 at 17:41











            • I got the cloning of fields bit to work with $("#plusanswer").prev().clone().insertBefore("#plusanswer"); but I get an error that it's expecting an array not a string. And my last field is the only one it takes. The others are getting ignored.

              – charliemagee
              May 11 '12 at 2:11












            • The server is complaining about a string where it expects an array?

              – mu is too short
              May 11 '12 at 2:45















            2














            If answers are just strings then you could use an array field:



            class Question
            include Mongoid::Document
            #...
            field :answers, :type => Array
            end


            If answers have some internal structure (perhaps you want to track when they were created or changed or whatever), then you could use embeds_many and two models:



            class Question
            include Mongoid::Document
            #...
            embeds_many :answers
            end

            class Answer
            include Mongoid::Document
            #...
            embedded_in :question
            end


            Either one will let you work with q.answers in a natural list-like way so rendering such things is a simple matter of <% q.answers.each do |a| %> and you could shuffle the answers to display them in a random order.






            share|improve this answer

























            • Thanks mu. I already made the :type => Array attempt, but what I'm struggling with is the form. I still only get one field for answers. I guess I was hoping that the scaffold would automagically create a little + plus sign next to the answer field. Click that and you get another answer field, and so on until you've got all your answer choices.

              – charliemagee
              May 9 '12 at 5:14






            • 1





              @charliemagee: AFAIK you have to do that yourself, it should only take a couple lines of jQuery, just a quick .clone and .append.

              – mu is too short
              May 9 '12 at 5:36











            • Thanks mu, I'll try that out.

              – charliemagee
              May 9 '12 at 17:41











            • I got the cloning of fields bit to work with $("#plusanswer").prev().clone().insertBefore("#plusanswer"); but I get an error that it's expecting an array not a string. And my last field is the only one it takes. The others are getting ignored.

              – charliemagee
              May 11 '12 at 2:11












            • The server is complaining about a string where it expects an array?

              – mu is too short
              May 11 '12 at 2:45













            2












            2








            2







            If answers are just strings then you could use an array field:



            class Question
            include Mongoid::Document
            #...
            field :answers, :type => Array
            end


            If answers have some internal structure (perhaps you want to track when they were created or changed or whatever), then you could use embeds_many and two models:



            class Question
            include Mongoid::Document
            #...
            embeds_many :answers
            end

            class Answer
            include Mongoid::Document
            #...
            embedded_in :question
            end


            Either one will let you work with q.answers in a natural list-like way so rendering such things is a simple matter of <% q.answers.each do |a| %> and you could shuffle the answers to display them in a random order.






            share|improve this answer















            If answers are just strings then you could use an array field:



            class Question
            include Mongoid::Document
            #...
            field :answers, :type => Array
            end


            If answers have some internal structure (perhaps you want to track when they were created or changed or whatever), then you could use embeds_many and two models:



            class Question
            include Mongoid::Document
            #...
            embeds_many :answers
            end

            class Answer
            include Mongoid::Document
            #...
            embedded_in :question
            end


            Either one will let you work with q.answers in a natural list-like way so rendering such things is a simple matter of <% q.answers.each do |a| %> and you could shuffle the answers to display them in a random order.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 21 at 18:01

























            answered May 9 '12 at 4:55









            mu is too shortmu is too short

            354k58701676




            354k58701676












            • Thanks mu. I already made the :type => Array attempt, but what I'm struggling with is the form. I still only get one field for answers. I guess I was hoping that the scaffold would automagically create a little + plus sign next to the answer field. Click that and you get another answer field, and so on until you've got all your answer choices.

              – charliemagee
              May 9 '12 at 5:14






            • 1





              @charliemagee: AFAIK you have to do that yourself, it should only take a couple lines of jQuery, just a quick .clone and .append.

              – mu is too short
              May 9 '12 at 5:36











            • Thanks mu, I'll try that out.

              – charliemagee
              May 9 '12 at 17:41











            • I got the cloning of fields bit to work with $("#plusanswer").prev().clone().insertBefore("#plusanswer"); but I get an error that it's expecting an array not a string. And my last field is the only one it takes. The others are getting ignored.

              – charliemagee
              May 11 '12 at 2:11












            • The server is complaining about a string where it expects an array?

              – mu is too short
              May 11 '12 at 2:45

















            • Thanks mu. I already made the :type => Array attempt, but what I'm struggling with is the form. I still only get one field for answers. I guess I was hoping that the scaffold would automagically create a little + plus sign next to the answer field. Click that and you get another answer field, and so on until you've got all your answer choices.

              – charliemagee
              May 9 '12 at 5:14






            • 1





              @charliemagee: AFAIK you have to do that yourself, it should only take a couple lines of jQuery, just a quick .clone and .append.

              – mu is too short
              May 9 '12 at 5:36











            • Thanks mu, I'll try that out.

              – charliemagee
              May 9 '12 at 17:41











            • I got the cloning of fields bit to work with $("#plusanswer").prev().clone().insertBefore("#plusanswer"); but I get an error that it's expecting an array not a string. And my last field is the only one it takes. The others are getting ignored.

              – charliemagee
              May 11 '12 at 2:11












            • The server is complaining about a string where it expects an array?

              – mu is too short
              May 11 '12 at 2:45
















            Thanks mu. I already made the :type => Array attempt, but what I'm struggling with is the form. I still only get one field for answers. I guess I was hoping that the scaffold would automagically create a little + plus sign next to the answer field. Click that and you get another answer field, and so on until you've got all your answer choices.

            – charliemagee
            May 9 '12 at 5:14





            Thanks mu. I already made the :type => Array attempt, but what I'm struggling with is the form. I still only get one field for answers. I guess I was hoping that the scaffold would automagically create a little + plus sign next to the answer field. Click that and you get another answer field, and so on until you've got all your answer choices.

            – charliemagee
            May 9 '12 at 5:14




            1




            1





            @charliemagee: AFAIK you have to do that yourself, it should only take a couple lines of jQuery, just a quick .clone and .append.

            – mu is too short
            May 9 '12 at 5:36





            @charliemagee: AFAIK you have to do that yourself, it should only take a couple lines of jQuery, just a quick .clone and .append.

            – mu is too short
            May 9 '12 at 5:36













            Thanks mu, I'll try that out.

            – charliemagee
            May 9 '12 at 17:41





            Thanks mu, I'll try that out.

            – charliemagee
            May 9 '12 at 17:41













            I got the cloning of fields bit to work with $("#plusanswer").prev().clone().insertBefore("#plusanswer"); but I get an error that it's expecting an array not a string. And my last field is the only one it takes. The others are getting ignored.

            – charliemagee
            May 11 '12 at 2:11






            I got the cloning of fields bit to work with $("#plusanswer").prev().clone().insertBefore("#plusanswer"); but I get an error that it's expecting an array not a string. And my last field is the only one it takes. The others are getting ignored.

            – charliemagee
            May 11 '12 at 2:11














            The server is complaining about a string where it expects an array?

            – mu is too short
            May 11 '12 at 2:45





            The server is complaining about a string where it expects an array?

            – mu is too short
            May 11 '12 at 2:45













            0














            If you want dynamically generated forms for nested models, I suggest following this RailsCast: http://railscasts.com/episodes/196-nested-model-form-part-1



            The RailsCast method uses application helpers to dynamically generate the new objects.



            I tend to prefer @mu's method of using jQuery to create form elements. The nice thing about Rails is that, when passing nested attributes, you can give it whatever index you want. And so I'll generate a new form with an index of, say, Time.now.to_s and no ID parameter. And so my controller receives a parameter that looks like this:



            "question" => "answers_attributes" => "1234567" => "description" => "New answer" 





            share|improve this answer



























              0














              If you want dynamically generated forms for nested models, I suggest following this RailsCast: http://railscasts.com/episodes/196-nested-model-form-part-1



              The RailsCast method uses application helpers to dynamically generate the new objects.



              I tend to prefer @mu's method of using jQuery to create form elements. The nice thing about Rails is that, when passing nested attributes, you can give it whatever index you want. And so I'll generate a new form with an index of, say, Time.now.to_s and no ID parameter. And so my controller receives a parameter that looks like this:



              "question" => "answers_attributes" => "1234567" => "description" => "New answer" 





              share|improve this answer

























                0












                0








                0







                If you want dynamically generated forms for nested models, I suggest following this RailsCast: http://railscasts.com/episodes/196-nested-model-form-part-1



                The RailsCast method uses application helpers to dynamically generate the new objects.



                I tend to prefer @mu's method of using jQuery to create form elements. The nice thing about Rails is that, when passing nested attributes, you can give it whatever index you want. And so I'll generate a new form with an index of, say, Time.now.to_s and no ID parameter. And so my controller receives a parameter that looks like this:



                "question" => "answers_attributes" => "1234567" => "description" => "New answer" 





                share|improve this answer













                If you want dynamically generated forms for nested models, I suggest following this RailsCast: http://railscasts.com/episodes/196-nested-model-form-part-1



                The RailsCast method uses application helpers to dynamically generate the new objects.



                I tend to prefer @mu's method of using jQuery to create form elements. The nice thing about Rails is that, when passing nested attributes, you can give it whatever index you want. And so I'll generate a new form with an index of, say, Time.now.to_s and no ID parameter. And so my controller receives a parameter that looks like this:



                "question" => "answers_attributes" => "1234567" => "description" => "New answer" 






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 10 '12 at 20:15









                RonRon

                1,138515




                1,138515



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10509632%2fpossible-to-have-model-in-mongoid-rails-with-a-field-that-varies-in-number%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    RBcy,UjSn0j jdh1 k9Phf686,7 IbjCx7It,I5 37d7dj,x8wJuaNjpnYwtDSuM SeEYunN4,5uYLr8OiDMkbExAIVfmajKzakLUI,uDC,Lw a
                    fTndxjUYCOUjbVIgc7DQjXQxhKYbj5M

                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현