Reputation: 12082
I do hope the title is correct. Using a Rails 5 project with PostgreSQL and Ruby 2.3.1.
I have enabled hstore in my app. I do not know the correct way to update a table column with an object data. Make sense?
# There will be only two arrays:
cars = ["honda", "bmw"]
rate = [1, 2]
cars.zip(rate).map do |c,r|
Foo.find(1).update_attributes(bar: {c => r})
end
# Foo.find(1).bar = {"bmw" => 2}
I expect:
# Foo.find(1).bar = {"honda" => "1", "bmw" => 2}
How to get the two values into bar
?
I was trying to fit one of my previous questions into this but not sure where to start.
Upvotes: 0
Views: 46
Reputation: 2114
You can create a hash from the array
a = cars.zip(rate)
Foo.find(1).update_attributes(bar: Hash[a])
or if you need some enumerator
cars.zip(rate).each_slice(2) { |x| Foo.find(1).update_attributes(bar: Hash[x]) }
Upvotes: 1