2011-09-22 65 views
0

我正在使用Rails 2.3.11。我有2級表的客戶和職位:錯誤NoMethodError(未定義方法'+'爲ActiveRecord :: Associations :: BelongsToAssociation:0xb6c9b45c)

# Table name: customers 
# id     :integer(4)  not null, primary key 
# Name    ::string(255)  default("Anonymous") 
... 

class Customer < ActiveRecord::Base 
    has_many :posts 
... 

# Table name: posts 
# id     :integer(4)  not null, primary key 
# customer_id   :integer(4) 
... 

class Post < ActiveRecord::Base 
    belongs_to :customer 
... 

在我posts_controller,我想回到一個GET調用的XML響應,相應的後交與客戶的詳細信息。

@customer = Customer Details 
@posting = Corresponding Post 

以下行引發錯誤NoMethodError (undefined method '+' for ActiveRecord::Associations::BelongsToAssociation:0xb6c9b45c)

respond_to do |format| 
    format.xml { render :xml => (@customer + @posting)} 

這看起來是一個很瑣碎的問題,我在這裏缺少一些基本知識。有人能幫我理解這個錯誤嗎?

回答

0

聽起來像是你需要這個:

format.xml { render :xml => @posting.to_xml(:include => :customer) } 

如果你想有一個客戶創造的職位可以使用:

format.xml { render :xml => @customer.to_xml(:include => :posts) } 

只是一個供參考。我會說出奇異實例版本你的對象相同..所以一個Post對象將是@post :)

相關問題