# Why can't to\_xml read my mind?

**URL:** <https://www.ruby-forum.com/t/why-cant-to-xml-read-my-mind/160982>\
**Category:** Rails\
**Created:** [February 21, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/why-cant-to-xml-read-my-mind/160982 "2009-02-21T19:49:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 21, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/why-cant-to-xml-read-my-mind/160982/1 "2009-02-21T19:49:40Z")

</div>

Railsters:

I suspect the answer is “because they invented it before inventing eager  
loading”.

The first minor issue with to\_xml’s refusal to read my mind is it’s not  
DRY  
(like my mind!):

Model.all(:include =\>{ :this =\> :that }).  
to\_xml(:include =\>{ :this =\> :that })

With eager loading, every Model instance has its internal @this member  
populated, and each This has its @that populated. So, instead of  
repeating the  
:include argument, to\_xml could just find every populated @this and  
@that  
instance variable, and recursively call to\_xml on it.

Do I misunderstand to\_xml here? Or is it indeed refusing to read my  
mind?

Another, more minor issue: to\_xml( :except =\>[:secret, :password] )

Is there, instead, a :pass argument, to positively declare what we _do_  
want?  
Negatively declaring the complete list of variables we _don’t_ want  
(being most  
of them) is not just a waste of time, it’s fragile if we migrate more  
secret  
fields into our model!

Please advise if I need to RFTM, grab a plugin, or else just live with  
to\_xml’s  
disturbing refusal to read my mind!

–  
Phlip

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 22, 2009, 12:41am UTC](https://www.ruby-forum.com/t/why-cant-to-xml-read-my-mind/160982/2 "2009-02-22T00:41:58Z")

</div>

On Feb 21, 6:48 pm, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> With eager loading, every Model instance has its internal @this member  
> populated, and each This has its @that populated. So, instead of repeating the  
> :include argument, to\_xml could just find every populated @this and @that  
> instance variable, and recursively call to\_xml on it.

I think it is hard for AR to distinguish between

m = Model.find …  
m.do\_something\_that\_causes\_association\_foo\_to\_be\_loaded  
m.to\_xml

m = Model.find …, :include =\> :foo  
m.to\_xml

In either case at the point that to\_xml is called the association is  
loaded - if it just spat every loaded association into the xml it  
would be very easy to accidentally include stuff. Or you might include  
an association at the point of loaded to make calling a certain method  
(which you do want included in the xml) faster. While I can certainly  
see your point I like to think of passing :include to find as nothing  
more than an optimisation hint ( I will be accessing the associations  
soon - you would do well to load them) that we make do with until AR  
gets smarter.

> Do I misunderstand to\_xml here? Or is it indeed refusing to read my mind?
> 
> Another, more minor issue: to\_xml( :except =\>[:secret, :password] )
> 
> Is there, instead, a :pass argument, to positively declare what we _do_ want?  
> Negatively declaring the complete list of variables we _don’t_ want (being most  
> of them) is not just a waste of time, it’s fragile if we migrate more secret  
> fields into our model!

That’s :only

Fred

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 22, 2009, 4:51pm UTC](https://www.ruby-forum.com/t/why-cant-to-xml-read-my-mind/160982/3 "2009-02-22T16:51:00Z")

</div>

Railsters:

I suspect the answer is “because they invented it before inventing eager  
loading”.

The first minor issue with to\_xml’s refusal to read my mind is it’s not  
DRY  
(like my mind!):

```
Model.all(:include =>{ :this => :that }).
  to_xml(:include =>{ :this => :that })

```

With eager loading, every Model instance has its internal @this member  
populated, and each This has its @that populated. So, instead of  
repeating the  
:include argument, to\_xml could just find every populated @this and  
@that  
instance variable, and recursively call to\_xml on it.

Do I misunderstand to\_xml here? Or is it indeed refusing to read my  
mind?

Another, more minor issue: to\_xml( :except =\>[:secret, :password] )

Is there, instead, a :pass argument, to positively declare what we _do_  
want?  
Negatively declaring the complete list of variables we _don’t_ want  
(being most  
of them) is not just a waste of time, it’s fragile if we migrate more  
secret  
fields into our model!

Please advise if I need to RFTM, grab a plugin, or else just live with  
to\_xml’s  
disturbing refusal to read my mind!

–  
Phlip

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 22, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/why-cant-to-xml-read-my-mind/160982/4 "2009-02-22T17:01:53Z")

</div>

Did my earlier reply not make it through ?

On 22 Feb 2009, at 15:46, Phlip wrote:

> Railsters:
> 
> I suspect the answer is “because they invented it before inventing  
> eager loading”.

(I don’t think so. eager loading was there way back in rails 1.0/1.1  
(possibly before, I just know it was there then because that’s when I  
hopped on board, to\_xml only really appeared with the whole restful  
drive (1.2 +))

Fred

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 22, 2009, 6:23pm UTC](https://www.ruby-forum.com/t/why-cant-to-xml-read-my-mind/160982/5 "2009-02-22T18:23:59Z")

</div>

Frederick C. wrote:

> I think it is hard for AR to distinguish between
> 
> m = Model.find …  
> m.do\_something\_that\_causes\_association\_foo\_to\_be\_loaded  
> m.to\_xml
> 
> m = Model.find …, :include =\> :foo  
> m.to\_xml

Then that’s what I mean by reading my mind.

In a desktop app, where ‘a’ might live for a long time, and go thru many  
uses,  
to\_xml should not guess that something\_ was for some other purpose. In  
an  
event-driven app, then ‘a’ should be populated, used, and discarded.

Maybe we could compromise on a bit inside AR saying “I was loaded  
eagerly -  
mind-reading is now okay!”

Or I could just invent this:

class ActiveRecord::Base  
def find\_to\_xml(include, conditions = {}, xml\_options = {})  
find(:all, conditions.merge(:include =\> include).  
to\_xml(xml\_option.merge(:include =\> include)  
end  
end

As an optimization hint, eager-loading might be more performant (yes  
it’s a real  
word, now), hence the best way to use to\_xml ought to be eagerly, hence  
we  
should get DRY about it.

While we have the subject open, where’s an example of to\_xml{}. The RDoc  
mentions it, of course, but does not illustrate it, of course.

> In either case at the point that to\_xml is called the association is  
> loaded - if it just spat every loaded association into the xml it  
> would be very easy to accidentally include stuff.

Our unit tests include checks that everything we don’t want is not  
there!

Uh… really!!

> > Another, more minor issue: to\_xml( :except =\>[:secret, :password] )
> > 
> > Is there, instead, a :pass argument, to positively declare what we _do_ want?  
> > Negatively declaring the complete list of variables we _don’t_ want (being most  
> > of them) is not just a waste of time, it’s fragile if we migrate more secret  
> > fields into our model!
> 
> That’s :only

Tx! Serves me right for reading RDoc output. I gotta cut down on that,  
huh?

–  
Phlip

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [February 22, 2009, 6:47pm UTC](https://www.ruby-forum.com/t/why-cant-to-xml-read-my-mind/160982/6 "2009-02-22T18:47:32Z")

</div>

On Feb 22, 5:23 pm, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Frederick C. wrote:
> 
> > In either case at the point that to\_xml is called the association is  
> > loaded - if it just spat every loaded association into the xml it  
> > would be very easy to accidentally include stuff.
> 
> Our unit tests include checks that everything we don’t want is not there!
> 
> Uh… really!!

Sure, but to me it would be very unobvious behaviour once you had got  
your failing unit test to see that the only code difference being that  
you called x.foo: calling an accessor (which feels like a read only /  
const thing) having a knock on effect on something unrelated would  
feel weird to me. Having the :include in find mark associations as  
being ok would be better.

Fred
