Reputation: 1181
I have the following hash holding a user's name
and items_sold
:
{"dave"=>9, "steve"=>20}
I created it in my Rawdatum Index
action through:
@consolidated_rawdata = Rawdatum.all.group(:user).sum(:items_sold)
What I want to do now is to save both objects from that hash (dave & steve) with a single action (a single click on a button) to a table called reports
with two columns: user
and items_sold
like so:
How can I achieve that?
Upvotes: 0
Views: 346
Reputation: 10111
if a action is just a single click then its really simple. We can just loop threw the hash and save the record
I would put this in a model like Reports
class Report < ApplicationRecord
#Report.create_consolidated_rawdata
def self.create_consolidated_rawdata
@consolidated_rawdata = Rawdatum.all.group(:user).sum(:items_sold)
@consolidated_rawdata.do |name, items_sold|
Report.create(user: name, items_sold: items_sold)
end
end
end
I hope that this works
Upvotes: 0
Reputation: 410
Don't know if that counts as a single action, but you can iterate through the hash like this:
@consolidated_rawdata.each {|name, items_sold| Report.create(user:
name, items_sold: items_sold)}
Upvotes: 1