Hope you could help me with the issue that I cannot find an answer. I have this app and trying to retrieve & update the value of record in DB.
My rake command in rails app looks like this:
task :nokogiri => :environment do
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::XML(open(url))
data = doc.at_css(".lister-list")
movies = data.css('.titleColumn')
current_time = Time.now.strftime("%d/%m/%Y %H:%M")
movies.each do |movie|
headline = movie.text.strip
found = Movie.where("title" => headline).first
if found.nil?
title = Movie.new
title._id = BSON::ObjectId.new
title.title = headline
title.current_time = current_time.to_s
title.save
puts "Record created"
else
# arr = found.to_a.to_json
# puts arr.title
puts found.title
puts "Record exist"
end
end
end
The issue is that I can output "arr" object to console, but I am not able to check title field. I keep getting:
rake aborted!
NoMethodError: undefined method `title' for #<String:0x007f68589d93e8>
What do I do wrong here? How I could update the record with current_time?
p.s. I am new on rails, so your help would help me a lot and you are the last ones who can help me.
THANK YOU!
Edited: Answer was simple, with function where, we have to limit results and put it .first. Then we can get object values as usual found.title
In the else
branch you say this:
arr = found.to_a.to_json
to_json
returns a String
. Then you say:
puts arr.title
but there is no String#title
method so you get a NoMethodError
. Perhaps you mean to say:
puts found.title
Also, do you really mean to say found.to_a.to_json
rather than found.to_json
? That to_a
call just wraps found
in a single element array.