has-many-throughself-joinbatman.js

Has many through self join in batmanjs


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?


Solution

  • 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!!