maccman/spine — Issue #164: a way to handle an "has and belong to many" association?
Author: loicginoux
Created: 2011-12-22 12:06:27+00:00
State: open

Hi,
I am building an app using Rails and Spine and I need a tagging system.
So let's say I have two models: Tags and Posts. In rails they are linked with an "has and belong to many" relationship.

#models/tag.rb
class Tag < ActiveRecord::Base
has_and_belongs_to_many :posts
end

#app/models/post.rb
class Tag < ActiveRecord::Base
has_and_belongs_to_many :tags
end

Everything is fine with Rails but now I am stuck because according to the [Spine doc](http://spinejs.com/docs/relations), this relation is not handled.
So maybe I need to declare both 'has many' and 'belongs to' for each spine models like:

#app/assets/javscript/app/models/tag.js.coffee
class Tag extends Spine.Model
@configure 'Tag', 'title'
@extend Spine.Model.Ajax

@belongsTo 'Post', 'Post'
@hasMany 'posts', 'Post'

I haven't tried this to be fair, it doesn't seem right.
I am wondering why this relation hasn't been done at the same time as the other relations, 'has many' and 'belongs to'? is it not possible or too complex? or is it just a question of time? in this last case I could help with implementing it.

I am probably not the first one who wants to use an habtm relation in Spine but I can't find any reference online about it.

Any more information would be really appreciated

Thanks