How to create has_and_belongs_to_many relationship with Ember.js & ember-data?
Asked Answered
P

1

8

Is it possible to create a hasAndBelongsToMany relationship with Ember.js & ember-data?

Edit: Added ActiveRecord model examples to clarify.

class Project < ActiveRecord::Base
  has_and_belongs_to_many :tags
end

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :projects
end

I have an associative table "projects_tags" linking project_id <=> tag_id.

Pouter answered 20/8, 2012 at 17:44 Comment(5)
I suppose you're asking about modeling a graph? Requisite functionality being asked about here (modeling a tree): #11997566 (I upvoted)Selfstyled
@pauldechov that post may be along the same lines of what I'm trying to achieve — I'm not too sure. I've added some ActiveRecord model examples of what I'm attempting to accomplish. Sorry for the lack of information originally.Pouter
There is no explicit HABTM in Ember. Here are the supported associations: github.com/emberjs/data/tree/master/packages/ember-data/lib/… You might try a has_many with an intermediate node/table similar to @pauldechov suggestion. Edit: Actually since the intermediate has_many would require an ID (in the rails way of thinking), I'm not sure that would work at all. :/Fireplug
Would a "has_many :through" relationship work?Tamarin
I've posted a kind of answer here, perhaps it could help for now, since many-to-many have not yet built-in support in ember-data #13300424Keldah
D
4

In this example, I'd suggest embedding the tags in the Post, so that the resulting structure looks like this

{
   title: "some post",
   tags: [ "tag1", "tag2" ]
}

This is generally good practice when you don't need to have additional value on the relation. If you want to have the Tag model on the client, you can still do it via embedding.

{
   title: "some post",
   tags: [ { name: "first tag" }, { name: "second tag" } ]
}

in this case you could specify it as an embedded relation, such as this

App.Post = DS.Model.extend({
  title: DS.attr("string"),
  tags: DS.hasMany("App.Tag")
});

App.Tag = DS.Model.extend({
  name: DS.attr("string"),
  post: DS.belongsTo("App.Post")
});

App.store.adapter.serializer.map("App.Tag", {
  tags: { embedded: 'load' }
});

keep in mind that embedding only works in Ember Data revision 10. It is not supported in 5-9.

Most of the time you don't really want to model everything the same way as you would model it in a relation database.

You should lean towards NoSQL-ish JSON, which generally means embedding things, rather than making complex associations. The reason for this is that your database can JOIN data very efficiently, especially with indexes, but doing this over the network is a bad idea because it results in a lot of requests.

Diffractive answered 19/12, 2012 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.