I run into these sort of to_xml problems all the time. Especially with nested models it gets really ugly, but I think for you just adding a line to your model should fix it.
...I've been working on an issue similar to this is on another thread but its a little more complicated because it uses order and a join, but your situation is similar. http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/82d640d37bdfb3cb?hl=en Here is how I would solve your issue. Add this line to your model class Library << ActiveRecord::Base has_many :books has_many :long_books, :class_name => "Book", :conditions => 'pages > 100' end Now update the controller to include :long_books Library.find(:all, :include => :long_books).to_xml (:include=>:long_books) On Jul 31, 2:42 pm, Colin Law <[email protected]> wrote: > 2009/7/31 Italo Matos <[email protected]>: > > > > > i want to list every libraries, > > but only show the your books with more than > > 100 pages. > > I understand now. I can not immediately see how to do that in one > query. Anyone else? > > You are _still_ top posting. It makes it much more difficult for > someone who has not been following the thread to pick it up and help. > > Colin > > > > > > > On 31 jul, 18:15, Colin Law <[email protected]> wrote: > >> 2009/7/31 Italo Matos <[email protected]>: > > >> > but, if me put the conditions in find method, > >> > it will filter much libraries ( if libraries has more one book with > >> > more than 100 pages ). > > >> I don't understand. Your original query would give you everything, the > >> modified one will only give you Libraries and books with greater than > >> 100 pages which is what I thought you asked for. To quote 'i would > >> like to create a xml with all Libraries, but books with pages more > >> than 100'. Perhaps I do not understand exactly what you mean, could > >> you give a more detailed example of what you want? > > >> Colin > > >> > On 31 jul, 17:10, Colin Law <[email protected]> wrote: > >> >> 2009/7/31 Italo Matos <[email protected]>: > > >> >> > I can`t put the conditionbs in the Library.find, because this > >> >> > relashionship is uses in other part of code. > >> >> > How to call named scope in this case? > > >> >> I meant use render :xml=> Library.find(:all, :include => :books, > >> >> :conditions => ['books.pages >= ?', 100]).to_xml(:include=>books) > > >> >> Or define a named_scope books_with_more_than_100_pages, though I > >> >> suspect you would actually parameterise this, then > >> >> render :xml=> > >> >> Library.books_with_more_than_100_pages.to_xml(:include=>books) > > >> >> The above not tested so probably full of typos. > > >> >> By the way it is generally frowned upon to top post in this list. > > >> >> Colin > > >> >> > On 31 jul, 16:44, Colin Law <[email protected]> wrote: > >> >> >> 2009/7/31 Italo Matos <[email protected]>: > > >> >> >> > i have: > > >> >> >> > class Library << ActiveRecord::Base > >> >> >> > has_many :books > >> >> >> > end > > >> >> >> > class Book << ActiveRecord::Base > >> >> >> > belongs_to :library > >> >> >> > end > > >> >> >> > in my controller i have: > > >> >> >> > render :xml=> Library.find(:all).to_xml(:include=>books) > >> >> >> > but, i would like put a condition in the books ( e.: pages >= 100 ) > > >> >> >> > i would like to create a xml with all Libraries, but books with > >> >> >> > pages more > >> >> >> > than 100) > > >> >> >> Just put the conditions in the Library.find call, ie ["books.pages >= > >> >> >> ?", 100] or similar. Or better still provide a named scope in Library > >> >> >> to do this. > > >> >> >> Colin --~--~---------~--~----~------------~-------~--~----~ 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

