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

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







                    Popular posts from this blog

                    How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

                    Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?