Reputation: 299
I am building a Ruby on Rails app that utilizes the Mongoid gem to store data through Mongodb.
My issue comes from this rake task:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
task :scrape => :environment do
page = Nokogiri::HTML(open('https://maps.google.com/maps/ms?ie=UTF8&hl=en&source=embed&dg=feature&authuser=0&msa=0&output=kml&msid=208523333872813891131.0004c02beb4f2788337d0'))
page.css("coordinates").each do |coords|
coords = coords.text.split("\n")
coords.each do |coord|
array = coord.strip.split(',')
array[0] = array[0].to_f
array[1] = array[1].to_f
if (array[0] != 0 && array[1] !=0)
puts array[0]
puts array[1]
s = Submission.new(array[0], array[1], "Calfire", nil)
s.insert
end
end
end
end
The s.insert
attempts to put the information taken from the URL, create an instance of a Submission
and then insert it into the Mongodb database. Each time I run rake scrape
in terminal, I get the following error as it tries to save the Submission
in the database.
-122.952888
40.741924
rake aborted!
Moped::Errors::OperationFailure: The operation: #<Moped::Protocol::Command
@length=83
@request_id=3
@response_to=0
@op_code=2004
@flags=[]
@full_collection_name="fireapp_development.$cmd"
@skip=0
@limit=-1
@selector={:getlasterror=>1, :w=>1}
@fields=nil>
failed with error 13066: "Message contains no documents"
I am fairly new to both Ruby and Mongodb so if any other code would be helpful, it can update this post with it. I tried setting up a new app/scaffolding to see if that would fix it but nothing changed. I also tried just saving an instance of Submission
through terminal but that didn't work either.
When googling for a solution I came across this discussion: https://groups.google.com/forum/#!topic/mongodb-user/kg-wK56_JkQ
It suggests that I might not be querying anything, but the puts
lines in the Ruby code clearly outputted into the terminal. Any help is greatly appreciated.
EDIT 1:
The fields for the Submission
model
class Submission
include Mongoid::Document
field :lat, type: Float
field :long, type: Float
field :image, type: Moped::BSON::Binary
field :category, type: String
end
EDIT 2:
The issues were fixed by properly formatting the s.create
line as well as removing the s.insert
line.
Upvotes: 3
Views: 203
Reputation: 434805
The standard usage is Model.create(attributes)
, you should be doing just:
s = Submission.create(:lat => array[0], :long => array[1], :category => 'Calfire')
# No Submission.new, no s.insert, just create and check s.valid? or use create!
I suspect that Submission.new
doesn't really know what to do with all those positional arguments so you're probably some sort of confused nonsense in s
. Presumable Submission.new
is trying to interpret the arguments and silently failing.
Upvotes: 2