Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails query: find first record by id and select only specified attributes for that record

I want to find a Blog by its :id, and then select only a couple of its attributes.

Example:

# Doesn't work and I don't know why
# I want to grab the first blog with this id, and then grab only selected attributes
blog = Blog.find(10).select(:id, :title, :date)

This post suggests to use where instead of find. But to my knowledge where in my scenario is inefficient because it continues searching the database for more matches even after it finds the only record I want to grab.

How can I specify to grab the first record it finds by the id, and then grab only the selected attributes?

like image 535
Neil Avatar asked Sep 17 '25 12:09

Neil


1 Answers

blog = Blog.select(:id, :title, :date).find(1)

like image 72
nsave Avatar answered Sep 19 '25 05:09

nsave