To_xml and the :include option

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

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

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. [email protected]

On Aug 14, 2:35 pm, tk [email protected] 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

On Aug 15, 6:48 am, tk [email protected] 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

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. [email protected]

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