I'm trying to create a function that validates data stored in my database. Say I have a table Foo. For each item in Foo i call a function say Bar which validates it by using a set of checks. If the data is not correct, I store the item id along with a description of the reason of failure in a hash. The hash is pushed on to an array.
ErrorList = []
MyHash = Hash.new {|h,k| h[k]=[]}
Foo.each do |f|
unless f.valid?
MyHash["foo_id"] = f.id
MyHash["description"] = "blah blah blah"
ErrorList.push MyHash
end
end
At the end of execution, all entries in the array are the same since the hash entries are overwritten. Is there a way I can use this hash to store different id's and description in my array. Otherwise, is there a way to use objects instead to overcome this issue?
I'm using rails version 2.3.5
First MyHash shouldn't be camel case, but you can just do...
myhash = {}
Foo.each do |f|
unless f.valid?
myhash[f.id] = "blah blah blah"
end
end
Assuming all the id's are unique, you only need this hash. This will set the key value pair to: id
: blah blah blah