# To\_xml and the :include option

**URL:** <https://www.ruby-forum.com/t/to-xml-and-the-include-option/145852>\
**Category:** Rails\
**Created:** [August 14, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/to-xml-and-the-include-option/145852 "2008-08-14T15:45:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [August 14, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/to-xml-and-the-include-option/145852/1 "2008-08-14T15:45:56Z")

</div>

Hi all

I’m having the following setup

Table: customerarticles  
belongs\_to: article  
foreign\_key: article\_id

Table: articles  
has\_many: customerarticles

Now I’m doing a search on the table articles and I’m returning the  
result using the to\_xml() function. If I specifiy to include the  
respective customerarticle I get the following message:

“You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.macro”

What am I missing? Any help?

Regarts tk

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [August 14, 2008, 4:03pm UTC](https://www.ruby-forum.com/t/to-xml-and-the-include-option/145852/2 "2008-08-14T16:03:05Z")

</div>

On 14 Aug 2008, at 14:45, tk wrote:

> has\_many: customerarticles
> 
> Now I’m doing a search on the table articles and I’m returning the  
> result using the to\_xml() function. If I specifiy to include the  
> respective customerarticle I get the following message:
> 
> “You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.macro”
> 
> What am I missing? Any help?

What are you passing to :include? It has to be the association name  
(ie :customerarticles if your model says has\_many :customerarticles)

Fred

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [August 14, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/to-xml-and-the-include-option/145852/3 "2008-08-14T21:35:45Z")

</div>

Actually as I’m searching through the customerarticles table I’m  
using :article. Isn’t that correct from that side?

On 14 Aug., 16:02, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [August 14, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/to-xml-and-the-include-option/145852/4 "2008-08-14T21:41:37Z")

</div>

On Aug 14, 2:35 pm, tk [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Actually as I’m searching through the customerarticles table I’m  
> using :article. Isn’t that correct from that side?

Can you show us the code that’s not working as you would expect?

Jeff

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [August 15, 2008, 10:41am UTC](https://www.ruby-forum.com/t/to-xml-and-the-include-option/145852/5 "2008-08-15T10:41:18Z")

</div>

On Aug 15, 6:48 am, tk [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Articles \< ActiveRecord::Base  
> has\_many :customerarticles  
> end

It’s quite possible getting confused because you’re messing up the  
conventions

1. class names should be singular
2. what’s after the belongs\_to (:article) should match up with the  
class name (but you’ve got Article).

Fred

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [August 15, 2008, 12:43pm UTC](https://www.ruby-forum.com/t/to-xml-and-the-include-option/145852/6 "2008-08-15T12:43:14Z")

</div>

Fantastic, thanks. As you see I’m just really a beginner in rails. It  
works like a charm now.

Thierry

On 15 Aug., 10:40, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [August 15, 2008, 7:49am UTC](https://www.ruby-forum.com/t/to-xml-and-the-include-option/145852/7 "2008-08-15T07:49:03Z")

</div>

Well yes.

Here is the customerarticles controller:

class Customerarticles \< ActiveRecord::Base  
belongs\_to :article  
end

Here is the article controller:

class Articles \< ActiveRecord::Base  
has\_many :customerarticles  
end

And here is the XML output:

#Searching customer specific articles  
def searchCustomerarticles

```
@result = Customerarticles.find(:all, :conditions => ["CUSTOMER_ID

```

like ?" , params[:customer][:id]])

```
respond_to do |format|
  if(@result.length>0)
    format.xml { render :partial => "shared/

```

actionResult", :locals =\> { :resultCode =\> “200”, :database =\>  
@result.to\_xml(:skip\_instruct =\> true, :include =\> [:article])}}  
else  
format.xml {render :partial =\> “shared/actionResult”, :locals  
=\> { :resultCode =\> “204” }}  
end  
end  
end
