I think the problem is that you only change the hash of the returned attribute, not the ActiveRecord object.
You need to do something like:
# make hash h @items.attributes = h
Following your example, perhaps something like:
@items.attributes = %w{type1 type2 type3 type4}.inject({}) { |m, e| m[e] = 'Test item'; m }
BTW, "#{e}" is the same as a String e expression or for any type: e.to_s . The second example may be easier to read:
a = %w{type1 type2 type3 type4} h = {} a.each { |name| h[name] = 'test item' } @items.attributes = h
Using the attributes= method is probably intended for hash constants, for example:
@items.attributes = { :field => 'value', :anotherfield => 'value' }
For fully created attributes, you can take the DanneManne clause and use send.
source share