belongsToMany #122

Closed
marcelklehr opened this Issue Sep 15, 2012 · 6 comments

Comments

Projects
None yet
4 participants
@marcelklehr

Hey,

how about a belongsToMany relation. This is useful for things like categories: A post belongs to many categories.

@robblovell

This comment has been minimized.

Show comment Hide comment
@robblovell

robblovell Jan 14, 2013

This would be really valuable!

This would be really valuable!

@marcelklehr

This comment has been minimized.

Show comment Hide comment
@marcelklehr

marcelklehr Jan 14, 2013

I agree.

I agree.

@robblovell

This comment has been minimized.

Show comment Hide comment
@robblovell

robblovell Jan 17, 2013

This should really be hasAndBelongsToMany for many to many relationships as in rails.

var User = describe('User', function () {
    property('email', String);
    property('name', String);
    property('createdAt', Date);
});
var Website = describe('Website', function () {
    property('name', String);
    property('url', String);
    property('subject', String);
    property('createdAt', Date);
});
var UserWebsite = describe('UserWebsite',function() {
    property('userId',String);
    property('websiteId',String);
});
User.hasAndBelongsToMany(Website, {as: 'memberships', linkTable: 'UserWebsite'});
Website.hasAndBelongsToMany(Users, {as: 'members', linkTable: 'UserWebsite'});

If the linkTable was not given, the linking table would be assumed to be the two class names concatenated together in alphabetical order.

If the "as" was not given it would be assumed to be the class name, all lowercase, with and 's' at the end. (no rails pluralization, just an s).

This should really be hasAndBelongsToMany for many to many relationships as in rails.

var User = describe('User', function () {
    property('email', String);
    property('name', String);
    property('createdAt', Date);
});
var Website = describe('Website', function () {
    property('name', String);
    property('url', String);
    property('subject', String);
    property('createdAt', Date);
});
var UserWebsite = describe('UserWebsite',function() {
    property('userId',String);
    property('websiteId',String);
});
User.hasAndBelongsToMany(Website, {as: 'memberships', linkTable: 'UserWebsite'});
Website.hasAndBelongsToMany(Users, {as: 'members', linkTable: 'UserWebsite'});

If the linkTable was not given, the linking table would be assumed to be the two class names concatenated together in alphabetical order.

If the "as" was not given it would be assumed to be the class name, all lowercase, with and 's' at the end. (no rails pluralization, just an s).

@robblovell

This comment has been minimized.

Show comment Hide comment
@robblovell

robblovell Jan 17, 2013

or maybe:

var UserWebsite = describe('UserWebsite',function() {
    property('userId',String);
    property('websiteId',String);
});
User.hasAndBelongsToMany(Website, {as: 'memberships', linkTable: 'UserWebsite', foreignKey: 'userId'});
Website.hasAndBelongsToMany(Users, {as: 'members', linkTable: 'UserWebsite', foreignKey: 'websiteId'});

or maybe:

var UserWebsite = describe('UserWebsite',function() {
    property('userId',String);
    property('websiteId',String);
});
User.hasAndBelongsToMany(Website, {as: 'memberships', linkTable: 'UserWebsite', foreignKey: 'userId'});
Website.hasAndBelongsToMany(Users, {as: 'members', linkTable: 'UserWebsite', foreignKey: 'websiteId'});
@anatoliychakkaev

This comment has been minimized.

Show comment Hide comment
@anatoliychakkaev

anatoliychakkaev Jan 19, 2013

Collaborator

On Fri, Jan 18, 2013 at 4:25 AM, Robb Lovell notifications@github.comwrote:

hasAndBelongsToMany

It would be the best to have 'hasAndBelongsToMany'.

Collaborator

anatoliychakkaev commented Jan 19, 2013

On Fri, Jan 18, 2013 at 4:25 AM, Robb Lovell notifications@github.comwrote:

hasAndBelongsToMany

It would be the best to have 'hasAndBelongsToMany'.

@1602

This comment has been minimized.

Show comment Hide comment
@1602

1602 Apr 15, 2013

Owner

Now 'hasAndBelongsToMany' first draft implemented. See readme for usage examples.

Owner

1602 commented Apr 15, 2013

Now 'hasAndBelongsToMany' first draft implemented. See readme for usage examples.

@1602 1602 closed this Apr 15, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment