0
votes

I've searched a lot for an answer to this, and I feel like I'm close...

I have Quiz, Question, and Answer models.

Rails--

Quiz has_many Questions

Question has_many Answers

Ember--

Quizzmob.Quiz = DS.Model.extend
    title: DS.attr('string')
    questions: DS.hasMany('question')


Quizzmob.Question = DS.Model.extend
    prompt: DS.attr('string')
    quiz: DS.belongsTo('quiz')

I'm able to persist a new quiz to my Rails backend just fine with a simple save(). I've also sideloaded the JSON so it looks like this:

{"questions":[
    {"id":214,"prompt":"Is this a question?"}  //--added manually--//
],
"quizzes":[{"id":185,"title":"First Quiz","questions":[214]},
    {"id":186,"title":"Second Quiz","questions":[]},
    {"id":187,"title":"Third Quiz","questions":[]},
    ...
    ...
]}

Here is a pattern I've seen a couple times in my search for an answer:

Ember QuestionsNewController:

Quizzmob.QuestionsNewController = Ember.ArrayController.extend(

    needs: 'quiz'
    quiz: Ember.computed.alias('controllers.quiz.model')
    prompt: ''

    actions:
        save: ->
            quiz = @get('controllers.quiz.content')
            quiz.save()
            prompt = @get('prompt')

            question = @store.createRecord "question",
                prompt: prompt

            @set('prompt', '')

            questions = quiz.get('questions')
            questions.addObject(question)
            question.save().then =>
                quiz.save()
)

My console shows all successful calls, my server shows successes too:

Processing by Api::V1::QuestionsController#create as JSON
Parameters: {"question"=>{"prompt"=>"new", "quiz_id"=>"185"}}
Completed 200 OK

Processing by Api::V1::QuizzesController#update as JSON
Parameters: {"quiz"=>{"title"=>"First Quiz"}, "id"=>"185"}
Completed 200 OK

A new Model for Question gets created and I see it in my Ember Debugger, but when I refresh the page, the model disappears. I read somewhere that it may have something to do with my serializer, and that I might have to add a 'serializeHasMany' method to customize it...but that seems like a lot of work for a seemingly simple and common task. Not sure if it's the 'ember way'.

Thanks so much for any help. I'll update the question with any additional info that's needed.

1

1 Answers

0
votes

I have a similar type of relationship in my database, and I think I see what your problem is. With ember, the relationship is not going to be saved on the hasMany side. So the relationship will be completely defined in your database in the questions table. To find all questions a quiz has, you would have to filter the database based on the paramter that the

{quiz_id: SOME_ID}

Now a question to ask yourself is can the questions on be on one quiz? If they can be on many quizzes, then you have a manyToMany relationship, which would probably require a change to the serializer, as I had to make myself.

In conclusion, the reason that your list of questions isn't being updated or sent to the server, is because you can only add the relationship to the Question itself, so question.set('quiz', some_quiz). You cannot get all questions from the quiz.

I hope this helps, sorry for all the wording.