I'm not sure if you're searching for a network that user2 is following, which has been created by user1 (easy) or if you're searching for a network that both users are following (a bit harder).
easy task user2.networks.where(creator: user1)
a bit harder task
I think the solution is described here: coderwall.com/p/9xk6ra/rails-filte...
So in your case it might look like this: user1.networks.merge(user2.networks)
In both cases the database does the hard work, so no need for iterating through all networks thus shooting N queries at the database.
In the first case user2.networks will give a list of networks he has created, but it seems like the way to go. I can have user2.followed_networks for that. Thanks
It's a bit different than you think. user2.networks does only return a list of networks when executed, because networks is not an array. It is an ActiveRecord::Relation. That's why user1.networks.merge(user2.networks) does something completely different than iterating through the networks.
I'm not sure if you're searching for a network that user2 is following, which has been created by user1 (easy) or if you're searching for a network that both users are following (a bit harder).
easy task
user2.networks.where(creator: user1)
a bit harder task
I think the solution is described here: coderwall.com/p/9xk6ra/rails-filte...
So in your case it might look like this:
user1.networks.merge(user2.networks)
In both cases the database does the hard work, so no need for iterating through all networks thus shooting N queries at the database.
In the first case
user2.networks
will give a list of networks he has created, but it seems like the way to go. I can haveuser2.followed_networks
for that. ThanksIt's a bit different than you think.
user2.networks
does only return a list of networks when executed, becausenetworks
is not an array. It is anActiveRecord::Relation
. That's whyuser1.networks.merge(user2.networks)
does something completely different than iterating through thenetworks
.I understand. What I wanted to say was that
user2.networks
is the list of networksuser2
has created, not the ones he's following.