Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a model and its associations in one request using Ember.js and Rails?
    primarykey
    data
    text
    <p>In ember.js/ember-data.js, is there a way to have the store POST to rails such that it sends information to create a model and its associations? Let me provide some context.</p> <p>Suppose I have 3 rails models:</p> <pre><code>class Post &lt; ActiveRecord::Base has_many :categorizations has_many :categories, :through =&gt; :categorizations attr_accessible :categories_attributes accepts_nested_attributes_for :categories end class Categories &lt; ActiveRecord::Base has_many :categorizations has_many :posts, :through =&gt; :categorizations end class Categorizations &lt; ActiveRecord::Base belongs_to :post belongs_to :categories end </code></pre> <p>In ember.js, I want to be able to create a post along with its categorizations in one request. This is what I have done to achieve that:</p> <pre><code>App.Category = DS.Model.extend name: DS.attr 'string' App.Categorization = DS.Model.extend post: DS.belongsTo 'App.Post' category: DS.belongsTo 'App.Category' App.Post = DS.Model.extend title: DS.attr 'string' content: DS.attr 'string' categorizations: DS.hasMany 'App.Categorization', embedded: true toJSON: (options={}) -&gt; options.associations = true @_super(options) # meanwhile, somewhere else in code... post = App.store.createRecord App.Post, title: "some title" content: "blah blah" transaction = App.store.transaction() categorization = transaction.createRecord App.Categorization, category: category # an instance of DS.Category post.get('categorizations').pushObject categorization # XXX: This enables ember-data to include categorizations in the post hash when # POSTing to the server so that we can create a post and its categorizations in # one request. This hack is required because the categorization hasn't been # created yet so there is no id associated with it. App.store.clientIdToId[categorization.get('clientId')] = categorization.toJSON() transaction.remove(categorization) App.store.commit() </code></pre> <p>I'm trying to make it so that when App.store.commit() is called it POSTs to /posts with something like:</p> <pre><code>{ :post =&gt; { :title =&gt; "some title", :content =&gt; "blah blah, :categorizations =&gt; [ # or :categorizations_attributes would be nice { :category_id =&gt; 1 } ] } } </code></pre> <p>Is there a way to achieve this without having ember POST to categorizations_controller to create the categorizations?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload