Has many through self join in batmanjs

56 Views Asked by At

I am trying to implement a has many through relationship as elaborated here. However, my related model is the same as the referring model by means of a self-join. I tried this:

class Article extends Batman.Model
  @hasMany 'citations'
  @hasMany 'usages', name: 'Citation', foreignKey: 'referenced_article_id'

  @accessor 'referenced_articles', ->
    @get('citations').mappedTo('referenced_article')

class Citation extends Batman.Model
  @belongsTo 'article'
  @belongsTo 'referenced_article', name: 'Article'

Unfortunately, calling my_article.get('referenced_articles') gives an error. Any ideas?

2

There are 2 best solutions below

1
On BEST ANSWER

Ah, shoot. I didn't add mappedTo to SetProxy in 0.16. It's fixed with this PR: https://github.com/batmanjs/batman/pull/1052

You could either get master from batmanjs.org/download.html or monkey-patch it with:

Batman.AssociationSet::mappedTo = Batman.Set::mappedTo

(That's what I was doing til I updated to master)

Sorry!!

1
On

Since your association is named usages (not citations), you could try:

@accessor 'referenced_articles', ->
  @get('usages').mappedTo('referenced_article')