# Would like include model association in to\_xml

**URL:** <https://www.ruby-forum.com/t/would-like-include-model-association-in-to-xml/173043>\
**Category:** Rails\
**Created:** [July 31, 2009, 9:36pm UTC](https://www.ruby-forum.com/t/would-like-include-model-association-in-to-xml/173043 "2009-07-31T21:36:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Italo\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@Italo\_M](https://www.ruby-forum.com/u/Italo_M)\
**Post date:** [July 31, 2009, 9:36pm UTC](https://www.ruby-forum.com/t/would-like-include-model-association-in-to-xml/173043/1 "2009-07-31T21:36:11Z")

</div>

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)

How to do this?

–  
Ãtalo Matos Cavalcante Santos  
Desenvolvedor Web/Mobile  
Analista de Sistemas  
[http://italomatos.wordpress.com/](http://italomatos.wordpress.com/) - Flex nos Trilhos  
[http://del.icio.us/italomatos](http://del.icio.us/italomatos)  
[http://www.activebrasil.com.br](http://www.activebrasil.com.br)

---

<div class="post-metadata">

**Author:** ![Italo\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@Italo\_M](https://www.ruby-forum.com/u/Italo_M)\
**Post date:** [July 31, 2009, 9:44pm UTC](https://www.ruby-forum.com/t/would-like-include-model-association-in-to-xml/173043/2 "2009-07-31T21:44:58Z")

</div>

2009/7/31 Italo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![Italo\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@Italo\_M](https://www.ruby-forum.com/u/Italo_M)\
**Post date:** [July 31, 2009, 9:54pm UTC](https://www.ruby-forum.com/t/would-like-include-model-association-in-to-xml/173043/3 "2009-07-31T21:54:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Italo\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@Italo\_M](https://www.ruby-forum.com/u/Italo_M)\
**Post date:** [July 31, 2009, 10:11pm UTC](https://www.ruby-forum.com/t/would-like-include-model-association-in-to-xml/173043/4 "2009-07-31T22:11:53Z")

</div>

2009/7/31 Italo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![Italo\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@Italo\_M](https://www.ruby-forum.com/u/Italo_M)\
**Post date:** [July 31, 2009, 10:52pm UTC](https://www.ruby-forum.com/t/would-like-include-model-association-in-to-xml/173043/5 "2009-07-31T22:52:15Z")

</div>

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 ).

---

<div class="post-metadata">

**Author:** ![Italo\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@Italo\_M](https://www.ruby-forum.com/u/Italo_M)\
**Post date:** [July 31, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/would-like-include-model-association-in-to-xml/173043/6 "2009-07-31T23:16:15Z")

</div>

2009/7/31 Italo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![Italo\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@Italo\_M](https://www.ruby-forum.com/u/Italo_M)\
**Post date:** [July 31, 2009, 11:34pm UTC](https://www.ruby-forum.com/t/would-like-include-model-association-in-to-xml/173043/7 "2009-07-31T23:34:25Z")

</div>

i want to list every libraries,  
but only show the your books with more than  
100 pages.

---

<div class="post-metadata">

**Author:** ![Italo\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@Italo\_M](https://www.ruby-forum.com/u/Italo_M)\
**Post date:** [July 31, 2009, 11:43pm UTC](https://www.ruby-forum.com/t/would-like-include-model-association-in-to-xml/173043/8 "2009-07-31T23:43:47Z")

</div>

2009/7/31 Italo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![Italo\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@Italo\_M](https://www.ruby-forum.com/u/Italo_M)\
**Post date:** [August 1, 2009, 12:59am UTC](https://www.ruby-forum.com/t/would-like-include-model-association-in-to-xml/173043/9 "2009-08-01T00:59:17Z")

</div>

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](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)
