I'm building a Rails app with an Ember front-end. I'm trying hard to ensure that the API is as plain, vanilla, RESTful and conventional as possible, in the Rails/Ember spirit.
I'm confused as to how to handle a situation where a parent model requires at least one child, and child models all require the existence of the parent.
Here's a somewhat contrived example:
class Meal < ActiveRecord::Base
has_one :appetizer
has_one :entree
has_many :side_dishes
has_many :desserts
# ensures there is AT LEAST one appetizer, entree, side dish or dessert
validate :plate_is_not_empty
accepts_nested_attributes_for :appetizer, :entree, :side_dishes, :desserts
end
class Appetizer < ActiveRecord::Base
belongs_to :meal
validates_presence_of :meal
end
class Entree < ActiveRecord::Base
belongs_to :meal
validates_presence_of :meal
end
class SideDish < ActiveRecord::Base
belongs_to :meal
validates_presence_of :meal
end
class Dessert < ActiveRecord::Base
belongs_to :meal
validates_presence_of :meal
end
In the past (aka pre-Ember, with a standard Rails app), my path was always to create the parent object through the parent controller (would have been MealsController, in this case), with at least one nested child.
snack = Meal.create(:name => 'Late night snack', :side_dishes_attributes => {:name => 'Corn Bread'})
then I was free to tack on additional children as needed, later on, as the parent had already been created.
Dessert.create(:name => "Marshmellow Fluff straight from the jar", :meal => snack)
But what's the best way to handle a situation like this with Ember? I'm assuming it's just a special case that'll require a custom ajax call with the nested attributes, which is fine. But I'm wondering how a situation like this fits into the emerging API standards that Ember is advocating. (And of which I'm a big fan.)