ruby on rails - How do you use map to get a hash instead of an array? -


I am currently using the following to get an array of certain fields in a table:

  classes.all.map (and: teacher_name)   

This gives it:

  ["James", "Josh" , "Peter"   

I wish there is something in return for a hash, where I can include teacher_ID:

  {"James "= & Gt; "1", "Josh" => "2", "Peter" => "3"}   

I tried to use the classrooms.all.map (& amp;: teacher_name, & amp;: teacher_id) Syntax error is returned.

Thank you!

Do it in a new way:

  pairs = Classrooms.all .map {| T | [T. TeacherName, T. Teacher_ID] # [key, value]} hash = hash [couple] #in / old / ruby: hash [* pairs.flatten]   

.. or whatnot

View

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -