Select a single column with active record

I have an ‘Image’ model and I needed to select only the ‘id’ column as an array.

ids = Image.pluck(:id)

You can easily combine this with a where clause.

ids = Image.where(field: 'value').pluck(:id)

Check the official documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation