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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
User.find(:all, :include=>:articles, :conditions=>["articles.id in (?)", [a1.id, a2.id].join('','')]) On Feb 26, 4:37 pm, Harry Seldon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
AndyV wrote:> User.find(:all, :include=>:articles, :conditions=>["articles.id in > (?)", [a1.id, a2.id].join('','')]) > > On Feb 26, 4:37 pm, Harry Seldon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>It works well. Thx a lot ! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Congrats! On Feb 28, 5:35 pm, Harry Seldon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> AndyV wrote: > > User.find(:all, :include=>:articles, :conditions=>["articles.id in > > (?)", [a1.id, a2.id].join('','')]) > > > On Feb 26, 4:37 pm, Harry Seldon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > It works well. Thx a lot ! > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Seldon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Harry Seldon 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 Various solutions to effectively the same problem. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Bush wrote:> Harry Seldon 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 > > Various solutions to effectively the same problem.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---