Hi
I got a model Category and another model Products. The have the relationship – has_and_belongs_to_many.
When I open the console and type Category.first.products, I receive a list with the products attached to that Category.
But when I try to generate an xml file with the show model I get:
undefined method `type' for nil:NilClass
I’ve tested the code below on other models I’ve made that has_many relationships and it works like how I want it to work. But won’t work with this relationship, maybe it doesn’t have anything to with that?
def show
@categories = Category.find(params[:id])
@products = @categories.products
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @products }
format.json { render :json => @products }
end
end
I’m new to this as you can see…
Thanks for your replies, I continued investigating and experimented with the controllers, and just tried with. @category.products.all, and it worked. The sad thing is that I don’t know why it did… Especially when it works for me in the console… But I guess I’m happy to have solved the problem in one way so to speak…
end
But I will use the log and the terminal logger when I debug more than before! Thanks for the tips!