Skip to content

How to filter an Association? #267

Closed

Description

@peruzzo

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(...);

Activity

  1. dresende commented on Aug 1, 2013

    @dresende
    Owner

    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.

  2. peruzzo commented on Aug 1, 2013

    @peruzzo
    Author

    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?

  3. dresende commented on Aug 1, 2013

    @dresende
    Owner

    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.

  4. taoyuan commented on Aug 5, 2013

    @taoyuan

    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.

  5. starwing commented on Aug 5, 2013

    @starwing

    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 :-)

  6. dresende commented on Aug 7, 2013

    @dresende
    Owner

    @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.

  7. takempf commented on Oct 24, 2013

    @takempf

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

  8. dresende commented on Oct 24, 2013

    @dresende
    Owner

    It depends on the place. Do you have examples?

  9. takempf commented on Oct 24, 2013

    @takempf

    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.

  10. dresende commented on Oct 30, 2013

    @dresende
    Owner

    Please try this latest commit.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions