Reputation: 7166
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...
Upvotes: 0
Views: 705
Reputation: 7166
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...
def show
@category = Category.find(params[:id])
@products = @category.products.all
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @products }
end
end
But I will use the log and the terminal logger when I debug more than before! Thanks for the tips!
Upvotes: 1
Reputation: 12840
In this case it looks like your problem is caused by some strange (maybe inconsistent) data. Try to see (in your console) the object which caused the problem - in your controller you have used some specific ID. Check whether Category.find(THIS_IS).products.to_xml
trows the error too.
Check whether a single Product is able to return something useful in response to .to_xml.
You will often meet similar problems. You should learn how to debug them.
First thing is to watch the backtrace. You probably see it in your web browser, but all exceptions usually are also placed in your log/development.log (depends on the environment).
In the development log you see the SQL queries run before the error. Sometimes they may help you to find what's wrong. In this case you may check what type of model was SELECT-ed just before the error. (this will be just a clue, not a solution)
In the backtrace, you watch for the files which you have edited. Usually the bug will be in your code. If your code looks OK, you may want to see the other files mentioned in the backtrace. Reading Rails' code may give you some clues. Sometimes.
And a question: have you messed with a property called 'type' in your models? If yes, then try to rename such attribute to something else.
We cannot give you more precise answer. I hope you will find the problem. Also, before you start fixing the error, try to write an unit test for this: check whether calling .to_xml on one of your models in test database does not raise an exception.
Upvotes: 0