# How to get the readers of 2 specific articles?

**URL:** <https://www.ruby-forum.com/t/how-to-get-the-readers-of-2-specific-articles/130268>\
**Category:** Rails\
**Created:** [February 26, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/how-to-get-the-readers-of-2-specific-articles/130268 "2008-02-26T22:37:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [February 26, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/how-to-get-the-readers-of-2-specific-articles/130268/1 "2008-02-26T22:37:59Z")

</div>

Hi,

I have 2 articles and I want to get the readers of both articles. How do  
I do that ?

Here is the pattern (from AWDWR):  
class Article \< ActiveRecord::Base  
has\_many :readings  
has\_many :readers, :through =\> :readings, :source =\> :user  
end

class User \< ActiveRecord::Base  
has\_many :readings  
has\_many :articles, :through =\> :readings  
end

Let’s say we have 2 articles : a1 and a2.

It is easy to get the readers of a1 or a2 :  
r1 = a1.readers  
r2 = a2.readers

Now, I want to get the readers of a1 AND a2 in one query. How can I do  
that simply ?

I know I could do a simple r1 & r2. But I am interested in the rails  
query (to add then some other conditions).

I would like to do something like :

User.find(:all, :conditions =\> “not(articles.find(a1.id).notempty?) &  
not(articles.find(a1.id).notempty?)”) (This does not work)

I do not know much about SQL. So If you directly see the SQL request to  
do that, it is interesting me too.

Thx  
H

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [February 27, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/how-to-get-the-readers-of-2-specific-articles/130268/2 "2008-02-27T16:21:05Z")

</div>

User.find(:all, :include=\>:articles, :conditions=\>[“articles.id in  
(?)”, [a1.id, a2.id].join(‘,’)])

On Feb 26, 4:37 pm, Harry S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [February 28, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/how-to-get-the-readers-of-2-specific-articles/130268/3 "2008-02-28T23:35:50Z")

</div>

AndyV wrote:

> User.find(:all, :include=\>:articles, :conditions=\>[“articles.id in  
> (?)”, [a1.id, a2.id].join(‘,’)])
> 
> On Feb 26, 4:37 pm, Harry S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

It works well. Thx a lot !

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [February 29, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/how-to-get-the-readers-of-2-specific-articles/130268/4 "2008-02-29T17:21:44Z")

</div>

Congrats!

On Feb 28, 5:35 pm, Harry S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [March 5, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/how-to-get-the-readers-of-2-specific-articles/130268/5 "2008-03-05T22:52:21Z")

</div>

I tested this too quickly. Now that I come back to this it appears that  
it is actually not working.

The command User.find(:all, :include=\>:articles,  
:conditions=\>[“articles.id in  
(?)”, [a1.id, a2.id].join(‘,’)]) returns the users who read one of the  
two articles but not only the users who read both.

Any idea of how to get simply the users who read both articles ?

Thx  
H

AndyV wrote:

> Congrats!
> 
> On Feb 28, 5:35 pm, Harry S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [March 5, 2008, 11:21pm UTC](https://www.ruby-forum.com/t/how-to-get-the-readers-of-2-specific-articles/130268/6 "2008-03-05T23:21:14Z")

</div>

Harry S. wrote:

> Any idea of how to get simply the users who read both articles ?

Have a look at this thread:

[http://www.ruby-forum.com/topic/143618](http://www.ruby-forum.com/topic/143618)

Various solutions to effectively the same problem.

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [March 6, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/how-to-get-the-readers-of-2-specific-articles/130268/7 "2008-03-06T22:36:05Z")

</div>

Thx, actually I have already seen this thread but clearly there is  
nothing “simple” in it.

This comes back to the question what is the SQL squery to get the users  
who read both articles.

For the moment I will simply do the intersection r1 & r2. It seems that  
Rails cannot do much for this question. So I will use SQL when I have  
some time.

Thx  
H

Mark B. wrote:

> Harry S. wrote:
> 
> > Any idea of how to get simply the users who read both articles ?
> 
> Have a look at this thread:
> 
> [Can anyone solve this sql query - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/143618)
> 
> Various solutions to effectively the same problem.
