I’m probably searching for the wrong thing here, and I’m dumb so let me explain.
I have a rake task that is polling a web service every 10 minutes via cron, the response is JSON.
For each of the items in the response I need to create a record only if a certain column has a unique value, in this case thats a hash.
Here’s the meat of the task:
response.parsed_response['items'].each do |item|
Item.create({
:item_hash => item['item_hash']
})
end
The thing is that I only want to do this if the hash value is unique, is this rudimentary behavior or am I missing some obvious method in ActiveRecord?
Use
find_or_createIf the data already exists Item is not created.
If you are processing lot of records I would use the following approach: