簡體   English   中英

在Ruby on Rails上替換返回的活動記錄哈希值

[英]Replacing returned active record hash value on Ruby on Rails

我有一個哈希,它是由Ruby on Rails活動記錄方法返回的。

有一個名為“ professional_type”的列,我想使用“ each”循環將其替換為另一個值。

但這不起作用。

編碼:

collection.each_with_index { |(key, value), index |

    if(key.professional_type == 'center_professional')

        key.professional_type = 'test' # does nothing
        key.professional_type << 'test' # this works and concats 'test' string to var

    end

}

這是collection.inspect結果:

[#<Professional id: 105, center_id: 20, name: "ABC", professional_type: "center_professional", created_at: "2014-05-28 12:21:48", updated_at: "2015-01-23 13:12:47", deleted_at: nil>, #<Professional id: 107, center_id: 20, name: "EDFG", professional_type: "center_professional", created_at: "2014-06-03 07:23:31", updated_at: "2015-01-23 13:15:35", deleted_at: nil>]

為什么如果我concat它將字符串添加到變量,但是如果我分配一個新值則它不起作用? 我做錯了什么?

謝謝大家

這應該工作-

collection.map { |key, value|

  if(value == 'center_professional')

    #concat key.professional_type + "<br>"
    collection[key] = 'test'
  end
}
collection.collect do  |professional| 
    professional.professional_type = 'test' if(value == 'center_professional') 
    professional
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM