Reputation: 23
I've just started Rails dev and have a question about inputing data into a serialized hash in my database from a url with a variable number of params. I'm trying to figure out how to implement a url with any number of param/values in the form (action?1=.5&2=.6&3=.1
etc) that I want to add to my serialized hash. If my hash already has some key/values previously stored, how do I add more to the same hash? I want all the data to be in one column.
I have a method where I pass it a model object:
def add_data_to_hash(obj)
params.each do |key, value|
if (key.to_i.is_a?(Integer) && key.to_i != 0) then
obj.hash[key] = value
obj.save
end
end
end
Thanks!
Upvotes: 2
Views: 479
Reputation: 1106
Something like this maybe?
this should go on the controller
new_values = {}
url = request.fullpath.split("?").last
url.split("&").each do |kv|
pair = kv.split("=")
new_values[pair[0].to_i] = pair[1]
end
new_value.delete 0
and something like this inside your method
new_values.each do |k,v|
object.hash[k] = v
end
object.save
Also it is better to call object.save once you finished updating the hash and not for every Integer key :)
Upvotes: 1