0

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?

Raymundus
  • 2,173
  • 1
  • 20
  • 35

2 Answers2

0

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

@accessor 'referenced_articles', ->
  @get('usages').mappedTo('referenced_article')
rmosolgo
  • 1,854
  • 1
  • 18
  • 23
  • Nope, still doesn't work. It looks like the `mappedTo` is not defined. The AssociationSet (usages) I am calling it from does have the related items (referenced articles). I also do have the latest version of batman (V0.16.0), so that shouldn't be the problem eighter. – Raymundus Aug 27 '14 at 17:01
0

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

rmosolgo
  • 1,854
  • 1
  • 18
  • 23
  • You can download batman/master from the website (http://batmanjs.org/download.html), but it has a few other breaking changes (see https://github.com/batmanjs/batman/issues/1106). In a pinch, you can just add that line ^^ right before your app definition. That how I fixed it before I fixed it in master. – rmosolgo Aug 28 '14 at 14:27