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.