ruby on rails - Nice array from pluck -
i have model , love pluck method can use. if this:
@x = awesomemodel.all.pluck(:column_one, :column_two)
then multidimensional array: @x[][]. sad skills, work them using numbers:
@x[0][1]
how can can use pluck or similar method access array this:
@x[0][:column_two]
if concerned structure of db, should do:
@x = awesomemodel.all.select(:column_one, :column_two)
then you'd keep fast db query advantage + have awesomemodel
instances, column_one
, column_two
filled
or if desire manually:
@x = awesomemodel.all.pluck(:column_one, :column_two).map |array| openstruct.new({column_one: array[0], column_two: array[1] }) } end
then can use regular model:
@x[0].column_one # or @x[0][:column_two]
Comments
Post a Comment