Ruby on Rails Friday, April 29, 2011

Hi, everyone.
I am using Geokit and Geokit-Rails3 on Ruby on Rails 3.0.3 / Ruby
1.8.7.

My model Location (which acts_as_mappable) is often created with lat
and lng.

For avoiding repeating yourself, I tried to override initialize method
and try to use Geokit::LatLng.normalize (this method creates a LatLng
object from string/array/etc..), but the following codes does not
work, and show curious errors.

There are also tests using the original style (:lat=>..., :lng=>...)
and they works fine.
According to "p ll" in location.rb, ll is correct.

Thanks in advance.

-- location.rb
class Location < ActiveRecord::Base
acts_as_mappable :default_units=>:kms, :default_formula=>:flat
attr_accessible :lat, :lng

def initialize(args)
begin
ll = Geokit::LatLng.normalize(args)
p ll
lat = ll.lat; lng = ll.lng
rescue
super
end
end
end

-- a part of location_spec.rb
it "should make new Location from LatLng object" do
Location.create!(Geokit::LatLng.normalize("35,135")).lat.should
== 35
end

it "should make new Location from String" do
Location.create!("35, 150").lat.should == 35
end

it "should make new Location from Array" do
p Location.new([35,150])#.lat.should == 35
end

-- errors (shortly)
NoMethodError: ... while evaluationg nil.[]
NoMethodError: undefined method `has_key?' for nil:NilClass (in `p')

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment