error NoMethodError (undefined method `+' for ActiveRecord::Associations::BelongsToAssociation:0xb6c9b45c)
I'm using Rails 2.3.11. I have 2 tables customers and posts:
# 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
...
In my posts_controller, I wanted to return the XML response for a GET call, with corresponding post and customer details.
@customer = Customer Details
@posting = Corresponding Post
Following line throws the error NoMethodError (undefined method '+' for ActiveRecord::Associations::BelongsToAssociation:0xb6c9b45c)
:
respond_to do |format|
format.xml { render :xml => (@customer + @posting)}
This looks to be a very开发者_高级运维 trivial issue and I'm missing some basics here. Can some one help me understand this error.
Sounds like you need this:
format.xml { render :xml => @posting.to_xml(:include => :customer) }
If you ever want the posts a customer has created you would use:
format.xml { render :xml => @customer.to_xml(:include => :posts) }
Just an FYI.. I would name the singular instance version of your object the same.. So a Post object would be @post :)
精彩评论