Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to filter an Association? #267

Closed
peruzzo opened this issue Aug 1, 2013 · 10 comments
Closed

How to filter an Association? #267

peruzzo opened this issue Aug 1, 2013 · 10 comments

Comments

@peruzzo
Copy link

peruzzo commented Aug 1, 2013

I want to be able to filter a association, as in the example below only bring the emails assets of a particular user.

var User =  db.define('user', ...);
var Email = db.define('email', ...);
Email.hasOne('user', User, {required: true, reverse: 'emails'});


user.getEmails().find({active: true}).run(...);
user.getEmails().where("active = ?", [true]).all(...);
@dresende
Copy link
Owner

dresende commented Aug 1, 2013

If you are using hasOne, you only get one association, no reason to filter. If you're using hasMany you can do exactly what you're proposing in your examples.

@peruzzo
Copy link
Author

peruzzo commented Aug 1, 2013

My intention is actually the reverse filter I set the hasOne, I thought the reverse applied a hasMany the user for email.

You said I was supposed to use the hasMany, but I can use without having a join table?

@dresende
Copy link
Owner

dresende commented Aug 1, 2013

I have to think how to change the interface to allow a reverse hasOne to be a hasMany. There's currently no support for it, but this is on the top of my list.

@taoyuan
Copy link

taoyuan commented Aug 5, 2013

Maybe the phenomenon described in #281 has the same reason. The hasOne and hasMany make me confused. I'm looking forward that this problem is resolved.

@starwing
Copy link

starwing commented Aug 5, 2013

and #237 as well.

Maybe it's very easy one: in interface, just makes reversed getAccessor return a ChainFind, so we can filter/count/limit, etc. on it.

in theory, a hasOne is a 1-to-n relation, a table A has a B_id, it means table B may have many different A instance. so at the reversed case, it should be a n-to-1 relation.

hopes to see this is implement :-)

@dresende
Copy link
Owner

dresende commented Aug 7, 2013

@starwing is right, a reversed hasOne should behave very similar to a normal hasMany. I'm hoping to fix this until the end of next week. Hopefully I'll also add more detail in the wiki about the differences in associations.

@Fauntleroy
Copy link

@dresende I've started to run into this issue in a variety of places. Has there been any progress on this?

@dresende
Copy link
Owner

It depends on the place. Do you have examples?

@Fauntleroy
Copy link

We have models User and Like. We have this association:

Like.hasOne( 'owner', User, {
    reverse: 'likes'
});

I think I should be able to use:

User.get( 1, function( err, user ){
    user.getLikes().limit(5).run( function( err, likes ){
        console.log( likes );
    });
});

But I can't since the limit method doesn't exist there.

dresende added a commit that referenced this issue Oct 30, 2013
Returns a ChainFind which can then be filtered..
@dresende
Copy link
Owner

Please try this latest commit.

@dxg dxg closed this as completed Jan 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants