Rails : Check if model has an item
I have a model called book. I want to be able to check if it has a book with the id of some number. It's a simple question, and I'm just learning. Do开发者_运维技巧n't be too harsh....
Well, it's simply:
Book.find_by_id(your_id)
It will render an object or nil.
Don't use Book.find(your_id)
, because it raises an exception if no book has the expected id.
I strongly advise you to check http://railsforzombies.org to learn Rails.
精彩评论