开发者

Rails : Check if model has an item

开发者 https://www.devze.com 2023-03-19 17:58 出处:网络
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 s

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

精彩评论

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