Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js / Rails and associations: how to submit back to Rails a record and its associations?

Say you have two Rails models with association:

class Foo < ActiveRecord::Base
  attr_accessible :name

  belongs_to :moo
end

class Moo < ActiveRecord::Base
  attr_accessible :name

  has_many :foos
  accepts_nested_attributes_for :foos
end

Thanks to ActiveModel::Serializer it is easy to send existing nested objects from Rails to Ember.js. But I can't find a way to create such nested objects in Ember and send them back to Rails for persistence. My understanding is Ember needs to send back something like:

"moo"=> {
   "foos_attributes"=>{"0"=>{"name" => ...}},
   "name" => ...
}

Is there a "standard" way to do that? I have seen a few "old" questions on this subject but no real solution, and since Ember has been changing so much I thought there might be a simple way to do it now (without changing the Rest Adapter itself).

Thanks a lot, PJ

like image 611
PJC Avatar asked Feb 08 '13 14:02

PJC


1 Answers

I have just modified the params before handing them to create or update_attributes methods like this:

  line_items = params[:invoice].delete(:line_items) #Ember format
  params[:invoice][:line_items_attributes] = line_items
like image 130
milep Avatar answered Oct 28 '22 23:10

milep