Closed
Description
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
dresende commentedon Aug 1, 2013
If you are using
hasOne
, you only get one association, no reason to filter. If you're usinghasMany
you can do exactly what you're proposing in your examples.peruzzo commentedon 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 commentedon 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 commentedon Aug 5, 2013
Maybe the phenomenon described in #281 has the same reason. The
hasOne
andhasMany
make me confused. I'm looking forward that this problem is resolved.starwing commentedon 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 commentedon 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 commentedon Oct 24, 2013
@dresende I've started to run into this issue in a variety of places. Has there been any progress on this?
dresende commentedon Oct 24, 2013
It depends on the place. Do you have examples?
Fauntleroy commentedon Oct 24, 2013
We have models
User
andLike
. We have this association:I think I should be able to use:
But I can't since the
limit
method doesn't exist there.Fixes reversed hasOne.getAccessor when called without callback (#267)
dresende commentedon Oct 30, 2013
Please try this latest commit.