开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜