Raymundus
Raymundus

Reputation: 2462

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?

Upvotes: 0

Views: 51

Answers (2)

rmosolgo
rmosolgo

Reputation: 1874

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

Upvotes: 0

rmosolgo
rmosolgo

Reputation: 1874

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

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

Upvotes: 0

Related Questions