Hi there!
Im new on ruby/rails, and im tying to run the rake task below but I
got an error. After I tested on script/console and I got the same
error. Could you helpe me, please?
Thanks!
Pedro
Rake Task:
namespace :meuApp do
desc ‘Update geotable with longitude and latitude information’
task :add_geotable_coordinates => :environment do
include GeoKit::Geocoders
geotable = Geotable.find_by_sql([“SELECT * FROM geotable WHERE
ADDRESS IS NOT NULL”])
begin
geotable.each { |geotable|
loc = MultiGeocoder.geocode(geotable.address)
geotable.lat = loc.lat
geotable.lng = loc.lng
geotable.update
puts “updated cartcid #{geotable.NOME}
#{geotable.address} =>
[#{loc.lat}, #{loc.lng}]”
}
rescue
puts $!
end
end
end
The script/console test:
Geotable.find_by_sql([“SELECT * FROM geotable WHERE ADDRESS IS NOT
NULL”])
…
… data retrieved ommited
…
@geotable.address
NoMethodError: undefined method `address’ for #Array:0x4712fa0
from (irb):27
You’re re-using your geotable variable inappropriately… try:
geotables = Geotable.find_by_sql([“SELECT * FROM geotable WHERE ADDRESS
IS NOT NULL”])
begin
geotables.each { |geotable|
loc = MultiGeocoder.geocode(geotable.address)
blah blah blah
namespace :meuApp do
desc ‘Update geotable with longitude and latitude information’
task :add_geotable_coordinates => :environment do
include GeoKit::Geocoders
geotable = Geotable.find_by_sql([“SELECT * FROM geotable WHERE
ADDRESS IS NOT NULL”])
begin
geotable.each { |geotable|
Yes, the names were the same. Now I changed the names for table,
collection, etc. Now, Im getting different error: undefined method
`address’ for #Array:0x4453c68
Sometimes this happens when the class can ot be instanciated, see if you
have a typo in the model somewhere
Yes, the names were the same. Now I changed the names for table,
collection, etc. Now, Im getting different error: undefined method
`address’ for #Array:0x4453c68
+±------------------------------------ new
code----------------------------------++
namespace :Geo do
desc ‘Update cartcid with longitude and latitude information’
task :add_cartcid_coordinates => :environment do
include GeoKit::Geocoders
c = Cartcid.find_by_sql([“SELECT address FROM cartcid WHERE
ADDRESS IS NOT NULL”])
begin
c.each { |cartcid|
loc = MultiGeocoder.geocode(c.address)
c.lat = loc.lat
c.lng = loc.lng
c.update
puts “updated cartcid #{c.nome} #{c.address} =>
[#{loc.lat}, #{loc.lng}]”
}
rescue
puts $!
end
end
end
namespace :Geo do
desc ‘Update cartcid with longitude and latitude information’
task :add_cartcid_coordinates => :environment do
include GeoKit::Geocoders
c = Cartcid.find_by_sql([“SELECT address FROM cartcid WHERE
ADDRESS IS NOT NULL”])
begin
c.each { |cartcid|
loc = MultiGeocoder.geocode(c.address)
c.lat = loc.lat
c.lng = loc.lng
c.update
puts “updated cartcid #{c.nome} #{c.address} =>
[#{loc.lat}, #{loc.lng}]”
}
rescue
puts $!
end
end
end
Still wrong… you did the exact same thing as before in another way…
c = Cartcid.find_by_sql(["SELECT address FROM cartcid WHERE
Given your code, “c” is an array (of Cardcid’s) - and the Array class
does not have an address attribute.
c.each { |cartcid|
Given this line, you are processing each individual element in “c” in a
variable named “cartcid”
loc = MultiGeocoder.geocode(c.address)
Now you are referencing the array “c” again!!! Just like you were doing
in your earlier post. That line needs to be:
Ok, now I got it, the code is below, but now I have the follow error:
Attempt to call private method
I dont know if it is related with this error, but this table is a
legacy and I had to set table name and primary key.
The model is :
class Cartcid < ActiveRecord::Base
set_table_name “cartcid”
set_primary_key “NUMERO”
end
namespace :GeoFind do
desc ‘Update cartcid with longitude and latitude information’
task :add_cartcid_coordinates => :environment do
include GeoKit::Geocoders
c = Cartcid.find_by_sql([“SELECT address, lat, lng FROM
cartcid WHERE ADDRESS IS NOT NULL”])
begin
c.each { |cartcid|
loc = MultiGeocoder.geocode(cartcid.address)
cartcid.lat = loc.lat
cartcid.lng = loc.lng
cartcid.update
puts “updated cartcid #{cartcid.nome}
#{cartcid.address} =>
[#{loc.lat}, #{loc.lng}]”
}
rescue
puts $!
end
end
end
Where you got told about that error, you would’ve been told the line
number… passing on that information would help in most cases (and
would tell you exactly where the problem is)
cartcid.update
“update” is a private method - you probably just want to .save
No. The problem is that because you’ve only selected the address
column rails can’t save the object, because it doesn’t know its id.
Secondly, what did you try in response to Fred pointing out you’d only
selected the address? You could use .find(:all) (or .all) or you could
“SELECT * …” in your find_by_sql.
No. The problem is that because you’ve only selected the address
column rails can’t save the object, because it doesn’t know its id.
Secondly, what did you try in response to Fred pointing out you’d
only
selected the address? You could use .find(:all) (or .all) or you
could
“SELECT * …” in your find_by_sql.
Im using find_by_sql([“SELECT address, lat, lng FROM
cartcid WHERE ADDRESS IS NOT NULL”]) because the table have 60.000
rows, with blob field. This choice was made thinking in performance
issues. So “select” would improve performance. Now I include the
primary key on select and it works.
Thanks to all of you,
Pedro
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.