Fernando Brito
2008-Jun-01 20:15 UTC
Books belongs_to authors. How to find only author with books
Hello I have a table called Books And a table called Authors Books belong_to authors How can I find only authors that have books? Thanks -- 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 -~----------~----~----~----~------~----~------~--~---
Rails Terrorist
2008-Jun-01 21:08 UTC
Re: Books belongs_to authors. How to find only author with b
Fernando Brito wrote:> Hello > > I have a table called Books > And a table called Authors > > Books belong_to authors > > How can I find only authors that have books? > > ThanksI think an author can write many books so that you can put has_many :books in Author ActiveRecord so that you can find books easily @author = Author.find(:first) @books = @author.books Reinhart http://teapoci.blogspot.com -- 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 -~----------~----~----~----~------~----~------~--~---
Kieran P
2008-Jun-01 21:30 UTC
Re: Books belongs_to authors. How to find only author with b
I thought I''d point out too that one book can have multiple authors, so you might need something like Book id name Writings book_id author_id Author id name and use something like Books has_many :authors, :through => :writings Writings belongs_to :book, belongs_to :author Author has_many :books, :through => :writings To get that many to many relationship working. As for your question, and using the above, now all you need to do is construct a find query to get authors if COUNT(*) FROM writings WHERE author_id = [id here] and check its size is > 0 That would be my solution anyway. A more exp. rails dev might have a faster option. Regards Kieran On Mon, Jun 2, 2008 at 9:08 AM, Rails Terrorist < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Fernando Brito wrote: > > Hello > > > > I have a table called Books > > And a table called Authors > > > > Books belong_to authors > > > > How can I find only authors that have books? > > > > Thanks > > > > I think an author can write many books > so that you can put has_many :books in Author ActiveRecord > > so that you can find books easily > > @author = Author.find(:first) > > @books = @author.books >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ramon Miguel M. Tayag
2008-Jun-02 02:34 UTC
Re: Books belongs_to authors. How to find only author with books
This is not optimal code, but you can make a method in Author: def self.find_all_with_books authors = [] for author in self.find(:all) authors << author if author.books.count > 0 end authors end If you''re using Rails 2.1 you can check out named_scope. Not familiar with it yet, but it goes something like this class Author < .... has_many :books named_scope :published, :include => :books, :conditions => ["books.count > 0"] I''m pretty sure the code won''t work. Watch this to be more familiar with it though http://railscasts.com/episodes/108 On Mon, Jun 2, 2008 at 4:15 AM, Fernando Brito <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hello > > I have a table called Books > And a table called Authors > > Books belong_to authors > > How can I find only authors that have books? > > Thanks > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ramon Tayag --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
sullivan.t
2008-Jun-02 12:27 UTC
Re: Books belongs_to authors. How to find only author with books
I like that, though what about finding the db records with sql and a standard table join? I suppose it''d be the same in terms of cpu cycles, etc. On Jun 1, 10:34 pm, "Ramon Miguel M. Tayag" <ramon.ta...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is not optimal code, but you can make a method in Author: > > def self.find_all_with_books > authors = [] > for author in self.find(:all) > authors << author if author.books.count > 0 > end > authors > end > > If you''re using Rails 2.1 you can check out named_scope. Not familiar > with it yet, but it goes something like this > > class Author < .... > has_many :books > named_scope :published, :include => :books, :conditions => ["books.count > 0"] > > I''m pretty sure the code won''t work. Watch this to be more familiar > with it thoughhttp://railscasts.com/episodes/108 > > On Mon, Jun 2, 2008 at 4:15 AM, Fernando Brito > > > > <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > Hello > > > I have a table called Books > > And a table called Authors > > > Books belong_to authors > > > How can I find only authors that have books? > > > Thanks > > -- > > Posted viahttp://www.ruby-forum.com/. > > -- > Ramon Tayag--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick DeNatale
2008-Jun-02 12:50 UTC
Re: Books belongs_to authors. How to find only author with books
On Mon, Jun 2, 2008 at 8:27 AM, sullivan.t <sullivan.t-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I like that, though what about finding the db records with sql and a > standard table join? > > I suppose it''d be the same in terms of cpu cycles, etc.No it wouldn''t, doing it in SQL would result in one query and less cycles. Something like: Author.find(:all, :joins => :books) Should do the trick, this should do a query with an inner equijoin on Authors and Books, so Authors without books would not be returned. Note that I haven''t actually tested this code. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jun-02 12:59 UTC
Re: Books belongs_to authors. How to find only author with books
On 2 Jun 2008, at 13:50, Rick DeNatale wrote:> On Mon, Jun 2, 2008 at 8:27 AM, sullivan.t <sullivan.t-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > I like that, though what about finding the db records with sql and a > standard table join? > > I suppose it''d be the same in terms of cpu cycles, etc. > > No it wouldn''t, doing it in SQL would result in one query and less > cycles. > > Something like: > > Author.find(:all, :joins => :books) > > Should do the trick, this should do a query with an inner equijoin > on Authors and Books, so Authors without books would not be returned. > > Note that I haven''t actually tested this code. >There''s always the cheat''s version: have a counter_cache on books. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ramon Miguel M. Tayag
2008-Jun-02 13:33 UTC
Re: Books belongs_to authors. How to find only author with books
Hey everyone! Well what do you know? I had to do this today, right after I emailed. If you''re going to use named_scope, you have to setup a counter cache. http://railscasts.com/episodes/23 then, in Author: named_scope :published, :conditions => ["books_count > 0"] if not named_scope, try: Author.find(:all, :conditions => ["books_count > 0"]) Either way, counter_cache would be a good thing to setup since doing these kinds of finds will be a lot less CPU intensive. -- Ramon Tayag --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---