简体   繁体   中英

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. Don'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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM