Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Getting an array of object ids by query/conversion (for comparision)

Basically, I want an array of ids from the database.

Some background:

  • I'm rendering a list of objets (foos) to the user with a checkbox.
  • If the user checks the box a row is create in a different table (bar)
  • when rendering the foo list + checkbox I want to check if the unique id any given foo already exists in the bar id array.

I assumed that getting an array of ids would be much more efficient than querying the database for each instance of foo when rendering the foo list with checkboxes.

Ideas? Apologies in advance if this is not clear.

like image 200
cbrulak Avatar asked Nov 28 '22 15:11

cbrulak


1 Answers

we can use the map method also this way

Model.all.map(&:field_name)
like image 155
Jyothu Avatar answered Dec 10 '22 05:12

Jyothu