Rails Import: "No Implicit conversion of string into array" - ruby-on-rails

I'm attempting to import values from a spreadsheet into a table. I am adding new records for each row to an array, and then importing that array. My Code is below:
def import(path)
spreadsheet = Roo::Spreadsheet.open(path+"Data.xlsx")
sales = spreadsheet.sheet('Accounts')
sales = sales.parse(headers: true)
accounts = []
sales.each do |row|
a = HM::NewBusiness.new
a.dealer_id = row["Dlr #"]
a.dealer_name = row["Dealer Name"]
a.duns = row["Duns Name"]
a.industry = row["Type"]
a.volume_2016 = row["volume_2016"]
a.volume_2017 = row["volume_2017"]
a.volume_2018 = row["volume_2018"]
a.volume_2019 = row["volume_2019"]
accounts << a
end
pp accounts
HM::NewBusiness.import(accounts)
end
However when I run import, I get:
TypeError: no implicit conversion of String into Array
I can't figure out where I'm going wrong. Any help would be appreciated.

Figured it out. The problem was that I coincidentally named the method itself "import". In short a 1d10t error.

Related

find_or_create_by ignore if column not nil

I have a rake file setup to import JSON data from an external URL. In this JSON there is a field titled 'deleted'. This field is a BIGINT, and if the field has been deleted then it will fill with random BIGINT.
I want to import all rows except those that have the 'deleted' field populated.
This is what I have so far, which works well except importing all rows.
data_json['Agent'].each do |data|
agent = Agent.find_or_create_by(agent_id: data['id'])
agent. agent_id = data['id']
agent.first_name = data['first_name']
agent.last_name = data['last_name']
agent.deleted = data['deleted']
agent.save
end
I believe what you're looking for it the next keyword. You could try something like this:
data_json['Agent'].each do |data|
agent = Agent.find_or_create_by(agent_id: data['id'])
agent. agent_id = data['id']
agent.first_name = data['first_name']
agent.last_name = data['last_name']
agent.deleted = !data['deleted'].nil? # Force this into a bool
next if agent.deleted?
agent.save
end

Array of hashes is overriding data directly to array

I want to make an array of hashes. But the problem is after first iteration when code goes to next line then it directly replaces the content of array.
#item_name =[]
item = {}
#invoiceinfo.each do |invoice|
item[:name] = Invoiceinfo.find(#invoiceinfo.id).item.name
item[:desc] = Invoiceinfo.find(#invoiceinfo.id).desc
item[:unit_price] = Invoiceinfo.find(#invoiceinfo.id).unit_price
byebug
#item_name.push (item)
end
This is what i am getting
after first iteration suppose i have this data
#item_name = [{:name=>"usman", :desc=>"sample ", :unit_price=>100}]
As soon as next line is executed it directly changes #item_name(name variable)
After executing item[:name] = Invoiceinfo.find(#invoiceinfo.id).item.name
the content of the #item_name is changed
#item_name = [{:name=>"next_name", :desc=>"sample ", :unit_price=>100}]
Any help would be appreciated.
Thannks
Try something like this
#item_name = []
#invoiceinfo.each do |invoice|
invoice_info = Invoiceinfo.find(#invoiceinfo.id)
item = {}
item[:name] = invoice_info.item.name
item[:desc] = invoice_info.desc
item[:unit_price] = invoice_info.unit_price
#item_name.push(item)
end
If you consider using ruby paradigms and best practices in ruby code, this mistake won’t happen in the future.
#item_name = #invoiceinfo.each_with_object([]) do |invoice, acc|
invoice_info = Invoiceinfo.find(#invoiceinfo.id)
acc.push(
name: invoice_info.item.name,
desc: invoice_info.desc
unit_price: invoice_info.unit_price
)
end

Rails no implicit conversion of Symbol into Integer(rails)

I am trying to save a data inside bookmark variable but it's throwing an error which is described in the title.Please Tell me what am I doing wrong I can't understand the error.
bookmarks = Bookmark.where(request_id: bookmarked_against_Request[:_id])
if bookmarks
bookmarks[:corsponding_requests] << request_bookmarked[:id]
else
bookmarks_new = Hash.new
bookmarks["owner_req"] = session[:user]
bookmarks["request_id"] = data
bookmarks["corsponding_requests"] = Array.new
bookmarks["corsponding_requests"] << request_bookmarked[:_id]
Bookmark.createBookmark(bookmarks)
end
PS:- I HAVE CHANGED BOOKMARKS TO BOOKMARKS_NEW IN THE ELSE BLOCK BUT IT STILL THROWING THE SAME ERROR
Try this:
bookmarks = Bookmark.where(request_id: bookmarked_against_Request[:_id])
if bookmarks
bookmarks.each { |bookmark| bookmark.update_attributes(corsponding_requests: request_bookmarked[:id] }
else
bookmark = Hash.new
bookmark["owner_req"] = session[:user]
bookmark["request_id"] = data
bookmark["corsponding_requests"] = Array.new
bookmark["corsponding_requests"] << request_bookmarked[:_id]
Bookmark.createBookmark(bookmark)
end
You can imagine bookmarks like array, and when you try bookmarks[:corsponding_requests] ruby "think", that you want acces to array element through integer index (for example like bookmarks[1]) but it can't convert symbol to integer and throw an error. Maybe, it helps you...

TypeError - no implicit conversion of Symbol into Integer

Hi I am building a an app with rails and angular. I keep on getting a type error executing this code
$scope.makeTip = function(tip){
data = {
tip: {
bookies: tip.bookies,
user_id: $scope.currentUser.id
},
prediction: $scope.madePredictions
},
$http.post('/tips.json', data).success(function(data){
console.log(data)
});
};
$http.get('/predictions/fixtures_this_week').success(function(response){
$scope.fixturesThisWeek = response.data;
});
//Updating the scores the dependent of on the type of bet id
$scope.addPrediction = function(prediction, fixtureId) {
data = {};
data.fixtureId = fixtureId;
data.predictionGoalsHomeTeam = prediction.scores.predictionGoalsHomeTeam[fixtureId];
data.predictionGoalsAwayTeam = prediction.scores.predictionGoalsAwayTeam[fixtureId];
data.typeOfBet = prediction.typeOfBetId[fixtureId];
$scope.madePredictions.push(data);
console.log($scope.madePredictions)
}
}]);
However I believe the problem stems from the method in my tips controller, possibly on the 4th line down
def create
#tip = Tip.new(params[:tip])
#tip.save
#prediction = Prediction.find(params[:prediction][:fixtureId])
#prediction.predictionGoalsHomeTeam = params[:prediction][:predictionGoalsHomeTeam]
#prediction.predictionGoalsHomeTeam = params[:prediction][:predictionGoalsAwayTeam]
#prediction.save
#tip.predictions << #prediction
respond_with(#tip)
end
Does anyone have any idea about how I can approach this type error?
Thanks
A couple of errors.. Inside your create method you are assigning #prediction.predictionGoalsHomeTeam twice in a row.
#prediction.predictionGoalsHomeTeam = params[:prediction][:predictionGoalsHomeTeam]
#prediction.predictionGoalsHomeTeam = params[:prediction][:predictionGoalsAwayTeam]
So you need to change the last one to AwayTeam as that is what you are getting from the params.
Your main issue is that params[:prediction] is an array so Prediction.find(params[:prediction][:fixtureId]) won't work as you are trying to grab the fixtureId of that array which doesn't exist. You need to loop through the params[:prediction] and store each object in #tip.predictions individually. Try using this:
def create
#tip = Tip.new(params[:tip])
#tip.save
params[:prediction].each do |p|
#prediction = Prediction.find(p[:fixtureId])
#prediction.predictionGoalsHomeTeam = p[:predictionGoalsHomeTeam]
#prediction.predictionGoalsAwayTeam = p[:predictionGoalsAwayTeam]
#prediction.save
#tip.predictions << #prediction
end
respond_with(#tip)
end

When scraping a website using Scrapy, how do I make sure all characters scrape properly?

I am using Scrapy to scrape a website but some of the characters, such as apostrophes, do not scrape correctly nor are they consistently the same wrong character, i.e., I've had an apostrophe show up as multiple odd characters in my result set. How do I ensure that all characters scrape properly?
Edit
I am trying to scrape http://www.nowtoronto.com/music/listings/ with the following scraper:
import urlparse
import time
from scrapy.http import Request
from scrapy.spider import BaseSpider
from scrapy.selector import Selector
#from NT.items import NowTorontoItem
from scrapy.item import Item, Field
class NowTorontoItem(Item):
eventArtist = Field()
eventTitle = Field()
eventHolder = Field()
eventDetails = Field()
#venueName = Field()
eventAddress = Field()
eventLocality = Field()
eventPostalCode = Field()
eventPhone = Field()
eventURL = Field()
eventPrice = Field()
eventDate = Field()
internalURL = Field()
class MySpider(BaseSpider):
name = "NTSpider"
allowed_domains = ["nowtoronto.com"]
start_urls = ["http://www.nowtoronto.com/music/listings/"]
def parse(self, response):
selector = Selector(response)
listings = selector.css("div.listing-item0, div.listing-item1")
for listing in listings:
item = NowTorontoItem()
for body in listing.css('span.listing-body > div.List-Body'):
item ["eventArtist"] = body.css("span.List-Name::text").extract()
item ["eventTitle"] = body.css("span.List-Body-Emphasis::text").extract()
item ["eventHolder"] = body.css("span.List-Body-Strong::text").extract()
item ["eventDetails"] = body.css("::text").extract()
#item ["internalURL"] = body.css("a::attr(href)").extract()
time.sleep(1)
for body in listing.css('div.listing-readmore'):
item ["internalURL"] = body.css("a::attr(href)").extract()
# yield a Request()
# so that scrapy enqueues a new page to fetch
detail_url = listing.css("div.listing-readmore > a::attr(href)")
if detail_url:
yield Request(urlparse.urljoin(response.url,
detail_url.extract()[0]),
meta={'item': item},
callback=self.parse_details)
else:
yield item
def parse_details(self, response):
self.log("parse_details: %r" % response.url)
selector = Selector(response)
listings = selector.css("div.whenwhereContent")
for listing in listings:
for body in listing.css('tr:nth-child(1) td.small-txt.dkgrey-txt.rightInfoTD'):
item = response.meta['item']
#item ["eventLocation"] = body.css("span[property='v:location']::text").extract()
#item ["eventOrganization"] = body.css("span[property='v:organization'] span[property='v:name']::text").extract()
#item ["venueName"] = body.css("span[property='v:name']::text").extract()
item ["eventAddress"] = body.css("span[property='v:street-address']::text").extract()
item ["eventLocality"] = body.css("span[property='v:locality']::text").extract()
item ["eventPostalCode"] = body.css("span[property='v:postal-code']::text").extract()
item ["eventPhone"] = body.css("span[property='v:tel']::text").extract()
item ["eventURL"] = body.css("span[property='v:url']::text").extract()
item ["eventPrice"] = listing.css('tr:nth-child(2) td.small-txt.dkgrey-txt.rightInfoTD::text').extract()
item ["eventDate"] = listing.css('span[content*="201"]::attr(content)').extract()
yield item
I am getting characters in some of the results like ée and é. These are supposed to be characters like é and ç.
Edit 2
I am not sure the issue is simply related to the file viewer I am using. When I open my first scrape in a text editor, an apostrophe is formatted as ’ whereas in my second scrape, the same apostrophe (from the same text string) is formatted as —È.
It turns out that the data is actually fine but the encoding was broken when I opened and saved the file in Excel. I have switched to Libre Office, which specifically asks for the encoding of the document when it is being opened, and everything works fine.

Resources