Hello, I am looking for a better way to do the following (my code examples are crappy pseudocode): I have two associated models, Book and Chapter, where book has_many chapters. I want to first select several books, for example, books_by_dr_seuss = Book.find(:all, :conditions => {:author => "Seuss" }) Then, I want to select all the "Chapter 3"''s from this result, i.e. Chapter 3 of Cat in the Hat, Chapter 3 of Fox in Socks, etc. I want to do something like: chapter_threes = Chapter.find(:all, :include=>[:book], :conditions => {chapter.book in books_by_dr_seuss } ) How would I do this without resorting to something like "select * from chapters where chapter.book_id=1 or chapter.book_id=2, etc"? Thanks for your help. A -- 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 -~----------~----~----~----~------~----~------~--~---
Wouldn''t it be something like the following: suess_chapter_three = Chapter.find(:all, :include=>:book, :conditions => [''books.auther like "suess" and chapters.chapter = 3'']) Simon On Fri, 13 Mar 2009 09:24:29 +0900, Avi Ir <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hello, > > I am looking for a better way to do the following (my code examples are > crappy pseudocode): > > I have two associated models, Book and Chapter, where book has_many > chapters. I want to first select several books, for example, > > books_by_dr_seuss = Book.find(:all, :conditions => {:author => "Seuss" > }) > > Then, I want to select all the "Chapter 3"''s from this result, i.e. > Chapter 3 of Cat in the Hat, Chapter 3 of Fox in Socks, etc. > > I want to do something like: > > chapter_threes = Chapter.find(:all, :include=>[:book], :conditions => > {chapter.book in books_by_dr_seuss } ) > > How would I do this without resorting to something like "select * from > chapters where chapter.book_id=1 or chapter.book_id=2, etc"? > > Thanks for your help. > A--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Simon Macneall wrote:> Wouldn''t it be something like the following: > > suess_chapter_three = Chapter.find(:all, :include=>:book, :conditions => > [''books.auther like "suess" and chapters.chapter = 3'']) > > SimonThanks for the response Simon -- I think my example didn''t fully capture what I am trying to do :) I want to select from a collection or list of items. I can''t use ''books.author like'' because there is no criteria that ties them all together. A better example would be: User is presented with a list of checkboxes (let''s say, book names) in a form. He checks several of these checkboxes. The form gets submitted and returns all of the chapters 3 from the selected books. For example say the user selects: Fox in Sock by Dr Seuss War and Peace by Leo Tolstoy The Godfather by Mario Puzo The form passes the ids of these selections (let''s say, [1000, 1001, 1002]) to the controller, which then calls a query on these selected plates: selected_books = params[:selected_books] Chapter.find(:all, :include=>:book, :conditions => ''chapter.book_id = book.id and book.id in books and chapters.chapter=3'' ) Is there a way to do that statement? "in books" is obviously not a valid command... Thanks! A -- 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 -~----------~----~----~----~------~----~------~--~---
I think something like the following: book_ids = ids of selected books (as gleened from the controller) chapter_threes = Chapter.find(:all, :include => :book, :conditions => [''book.id in ? and chapter.chapter_number = 3'', book_ids]) obviously not tested, but once you fix any syntax errors it should work. Simon On Fri, 13 Mar 2009 09:46:14 +0900, Avi Ir <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Thanks for the response Simon -- I think my example didn''t fully capture > what I am trying to do :) > > I want to select from a collection or list of items. I can''t use > ''books.author like'' because there is no criteria that ties them all > together. > > A better example would be: > User is presented with a list of checkboxes (let''s say, book names) in a > form. He checks several of these checkboxes. The form gets submitted > and returns all of the chapters 3 from the selected books. > > For example say the user selects: > > Fox in Sock by Dr Seuss > War and Peace by Leo Tolstoy > The Godfather by Mario Puzo > > The form passes the ids of these selections (let''s say, [1000, 1001, > 1002]) to the controller, which then calls a query on these selected > plates: > > selected_books = params[:selected_books] > Chapter.find(:all, :include=>:book, :conditions => ''chapter.book_id > book.id and book.id in books and chapters.chapter=3'' ) > > Is there a way to do that statement? "in books" is obviously not a > valid command... > > Thanks! > A--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank you for the reply Simon. What you said worked for me, although I did it in a rather inelegant way -- I''m not sure about the correct syntax of my solution, but it works. It requires creating a string with the ids in parens... book_ids = "(100, 101, 102)" chapters= Chapter.find(:all, :include => :books, :conditions => ["books.id in #{book_ids} and chapter=''2''"] Any suggestions for clearer code welcome, but thank you for your help! -- 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 -~----------~----~----~----~------~----~------~--~---
On Mar 18, 5:02 pm, Avi Ir <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thank you for the reply Simon. What you said worked for me, although I > did it in a rather inelegant way -- I''m not sure about the correct > syntax of my solution, but it works. It requires creating a string with > the ids in parens... > > book_ids = "(100, 101, 102)" > chapters= Chapter.find(:all, :include => :books, :conditions => > ["books.id in #{book_ids} and chapter=''2''"] >first off that can be done nicer: book_ids = [1,2,3] chapters= Chapter.find(:all, :include => :books, :conditions => ["books.id in (?) and chapter=''2''", book_ids] or even chapters= Chapter.find(:all, :include => :books, :conditions => { ''books.id'' => book_ids, ''chapter'' => 2}) You don''t even need to get an array of book_ids - an array of activerecord objects will do fine too. And last of all: Chapter.find :all, :joins => :books, :conditions => {''books.author'' => ''Seuss'', ''chapter'' => 2} Fred> Any suggestions for clearer code welcome, but thank you for your help! > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Fred, Thanks for your response -- Your second example is exactly what I needed. The final example, though -- why do you use :joins instead of :include? -- 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 -~----------~----~----~----~------~----~------~--~---