Feed Array of Strings into Ruby Loop - ruby

I'd like to write a quick method that can help me initialize a few fields in one of my ruby tables. This is what I have so far but it does not work. I would like to be able to feed an array of field names into this function so that I can get the whole initialization done in one loop.
fields =["field1","field2","field3","field4"]
tasks = Task.all
tasks.each do |task|
fields.each do |field|
if task.field.nil?
task.update_attribute :field => true
end
end
end

Maybe this is what you mean:
fields = %w[field1 field2 field3 field4]
tasks = Task.all
tasks.each do |task|
fields.each do |field|
task.update_attribute :"#{field}" => true if task.send(field).nil?
end
end

If this is actually Rails, as it appears to be, you can use hash access:
task[field] = true if task[field].nil?
You would still need to save the modified record.
You may use task.update_attribute(field, true) instead: this will update the database immediately, but will do a transaction for each modified attribute.

Try to always use the least number of queries to the database
fields = ["field1","field2","field3","field4"]
fields.each do |field|
Task.where({field => nil}).update_all({field => true})
end

Related

Mongoid Is it possible to override updated_at timestamp

I am trying to manually edit the "updated_at" field through a rake task
Here is what it looks like:
task :campaigns_updated_at_recovery => :environment do
Dir.foreach('db/raw-data/campaigns/') do |json|
next if json == '.' or json == '..'
file = File.read('db/raw-data/campaigns/'+json)
data_hash = JSON.parse(file)
#p data_hash["_id"]
thisCampaign = Campaign.find(data_hash["_id"])
thisCampaign["channels"].each do |chan|
if chan["updated_at"] < Date.new(2018,04,19)
data_hash["channels"].each do |channel|
if chan["_id"].to_s == channel["_id"]
chan["updated_at"] = Date.parse(channel["updated_at"])
end
end
end
thisCampaign.save
end
end
However when I run this task, the updated_at date is either not changed or updated to today's date.
What am I missing ?
I am using Mongoid and not ActiveRecord by the way
updated_at is updated by mongoid itself in a callback.
You have two solution to work around that.
The easiest solution would be to use set to change the value directly without firing any callback:
thisCampaign.set(channels: thisCampaign['channels'])
The more flexible solution would be to go down to the driver level. The basic idea is:
Campaign.collection.find(_id: data_hash["_id"]).update_one({
'$set' => {
updated_at: yourDate
}
})
Given your example, you would first need to get the full document
thisCampaign = Campaign.collection.find(_id: data_hash["_id"]).first
if thisCampaign
thisCampaign["channels"].each do |chan|
if chan["updated_at"] < Date.new(2018,04,19)
data_hash["channels"].each do |channel|
if chan["_id"].to_s == channel["_id"]
chan["updated_at"] = Date.parse(channel["updated_at"])
end
end
end
end
Campaign.collection.find(_id: data_hash["_id"]).update_one({
'$set' => {channels: thisCampaign["channels"]}
})
end

How to parse a Hash of Hashes from a CSV file

I have a CSV file that I need to read and extract all rows which have a "created_at" within a certain range. The CSV itself is about 5000 lines in Excel.
This is how I am pulling the info from the file:
CSV.foreach("sample_data.csv", :headers => true, :header_converters => :symbol, :converters => :all) do |row|
data[row.fields[0]] = Hash[row.headers[1..-1].zip(row.fields[1..-1])]
end
Here's the last Hash created after using CSV.foreach:
2760=>{:created_at=>1483189568, :readable_date=>"12/31/2016", :first_name=>"Louise", :last_name=>"Garza", :email=>"lgarza24n#drupal.org", :gender=>"Female", :company=>"Cogilith", :currency=>"EUR", :word=>"orchestration", :drug_brand=>"EPIVIR", :drug_name=>"lamivudine", :drug_company=>"State of Florida DOH Central Pharmacy", :pill_color=>"Maroon", :frequency=>"Yearly", :token=>"_", :keywords=>"in faucibus", :bitcoin_address=>"19jTjXLPQUL1nEmHrpqeqM1FdtDFZmUZ2E"}}
When I run data[2759].first I get:
created_at
1309380645
I need to pull every hash where created_at is between range = 1403321503..1406082945. I tried about twenty different methods using each and collect on the data hash with no success. My last attempt printed out an empty {} for each original hash.
I'm trying to test this with no success:
data.each do |hash|
if hash.first.to_s.to_i > 1403321503 && hash.first.to_s.to_i < 1406082945
puts hash
end
end
I'm not sure how to isolate the value of key:created_at and then see if it is within the range. I also tried doing hash.first.to_s.to_i =/== range.
I am able to get just the :created_at value by using data[1].first.last but when I try to use that in a method it errors out.
Here is a link to the original CSV: goo.gl/NOjAPo
It is not on my work computer so I can't do a pastebin of it.
I would only store rows in the data hash that are within the range. IMO that performs betters, because it needs less memory than reading all data into data and remove the unwanted entries in a second step.
DATE_RANGE = (1403321503..1406082945)
CSV.foreach("sample_data.csv",
:headers => true,
:header_converters => :symbol,
:converters => :all) do |row|
attrs = Hash[row.headers[1..-1].zip(row.fields[1..-1])]
data[row.fields[0]] = attrs if DATE_RANGE.cover?(attrs[:created_at])
end
It might make sense to check the condition before actually creating the hash by checking DATE_RANGE.cover? against the column number (is created_at in row.fields[1]?).
Use Enumerable#select
hash.select do |_, v|
(1403321503..1406082945) === v[:created_at]
end
Here we also use Range#=== also known as case-equal, or triple-equal, to check if the value is inside the range.

How to remove a row from a CSV with Ruby

Given the following CSV file, how would you remove all rows that contain the word 'true' in the column 'foo'?
Date,foo,bar
2014/10/31,true,derp
2014/10/31,false,derp
I have a working solution, however it requires making a secondary CSV object csv_no_foo
#csv = CSV.read(#csvfile, headers: true) #http://bit.ly/1mSlqfA
#headers = CSV.open(#csvfile,'r', :headers => true).read.headers
# Make a new CSV
#csv_no_foo = CSV.new(#headers)
#csv.each do |row|
# puts row[5]
if row[#headersHash['foo']] == 'false'
#csv_no_foo.add_row(row)
else
puts "not pushing row #{row}"
end
end
Ideally, I would just remove the offending row from the CSV like so:
...
if row[#headersHash['foo']] == 'false'
#csv.delete(true) #Doesn't work
...
Looking at the ruby documentation, it looks like the row class has a delete_if function. I'm confused on the syntax that that function requires. Is there a way to remove the row without making a new csv object?
http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Row.html#method-i-each
You should be able to use CSV::Table#delete_if, but you need to use CSV::table instead of CSV::read, because the former will give you a CSV::Table object, whereas the latter results in an Array of Arrays. Be aware that this setting will also convert the headers to symbols.
table = CSV.table(#csvfile)
table.delete_if do |row|
row[:foo] == 'true'
end
File.open(#csvfile, 'w') do |f|
f.write(table.to_csv)
end
You might want to filter rows in a ruby manner:
require 'csv'
csv = CSV.parse(File.read(#csvfile), {
:col_sep => ",",
:headers => true
}
).collect { |item| item[:foo] != 'true' }
Hope it help.

Ruby - Dynamically named variable based on a string read from a .csv

I have a .csv from which I read and parse to create an instance of a class, I want to name the class after the string of text returned from the first row in the .csv.
I can create the classes just fine manually but want to read row[0] and name the variable after that.
eg.
CSV.foreach("banks.csv", :headers => true) do |row|
***contents of row[0]*** = Bank.new(row[0], row[1], row[2], row[3], row[4])
row[0] == "Bank_of_America" for example, so I want the code to be equivalent to the following;
Bank_of_America = Bank.new(row[0], row[1], row[2], row[3], row[4])
I have read a few other replies on similar topics using instance_variable_set but cannot get the code given to work.
Thanks in advance for any advice!
edit: The following worked;
instance_variable_set("##{row[0]}", Bank.new(row[0], row[1], row[2], row[3], row[4]))
You could do something like this and create an instance variable dynamically:
x = 10
# => 10
instance_variable_set("#the_number_#{x}", x)
# => 10
#the_number_10
# => 10
You can replace "#the_number_#{x}" with row[0] now.
EDIT: Sorry, I didn't read the last part of you question about instance variables. So, how do you mean you can't get them to work? Does it come up with some kind of exception or just doesn't set to the right value. Give us the code that doesn't work.

With Mongoid, can I "update_all" to push a value onto an array field for multiple entries at once?

Using Mongoid, is it possible to use "update_all" to push a value onto an array field for all entries matching a certain criteria?
Example:
class Foo
field :username
field :bar, :type => Array
def update_all_bars
array_of_names = ['foo','bar','baz']
Foo.any_in(username: foo).each do |f|
f.push(:bar,'my_new_val')
end
end
end
I'm wondering if there's a way to update all the users at once (to push the value 'my_new_val' onto the "foo" field for each matching entry) using "update_all" (or something similar) instead of looping through them to update them one at a time. I've tried everything I can think of and so far no luck.
Thanks
You need call that from the Mongo DB Driver. You can do :
Foo.collection.update(
Foo.any_in(username:foo).selector,
{'$push' => {bar: 'my_new_val'}},
{:multi => true}
)
Or
Foo.collection.update(
{'$in' => {username: foo}},
{'$push' => {bar: 'my_new_val'}},
{:multi => true}
)
You can do a pull_request or a feature request if you want that in Mongoid builtin.

Resources