I’m trying to make use of both :only and :include options in to_xml call in a Rails app. Here’s the code:
current_user.to_xml(
:include => :subscription,
:only => [:email, :username]
)
The result of this is somthing like:
<?xml version="1.0" encoding="UTF-8"?>
<user>
<username>cristi</username>
<email>cristi@example.com</email>
<subscription>
</subscription>
</user>
The problem is that subscription has more fields, which are not included. I assume its because of the :only option.
Is there a way to overcome this (show all fields in the subsscription element), without using :except as an option in to_xml ?
I am using Rails 3.
Yes, you can. It works perfectly for me.
Make sure the
:subscriptionassociation is correct. If it’s a one-to-many association, you need to use the pluralized version:subscriptions.