Rails 2.3.4 Geokit error? - ruby-on-rails

I have a rails application. Rails version is 2.3.4 and ruby version 1.8.7. In that application I used geokit (1.5.0) gem and geokit-rails plugin to find nearest location.
And in my model I have code like this,
class Notary < ActiveRecord::Base
acts_as_mappable
end
And in my controller have code like this,
class Client::OrdersController < ApplicationController
def find_notary
#order = Order.find(params[:id])
#miles = 10
unless params[:notary_search]
#notaries = Notary.find(:all, :origin => #order.signing_location_zip_code, :within => #miles, :conditions => "on_vacation IS NOT true",:order=>"distance asc" )
else
some code
end
end
end
I am getting error in that line
#notaries = Notary.find(:all, :origin =>
#order.signing_location_zip_code, :within => #miles, :conditions =>
"on_vacation IS NOT true",:order=>"distance asc") like this,
Geokit::Geocoders::GeocodeError in
Client/ordersController#find_notary
Geokit::Geocoders::GeocodeError
RAILS_ROOT: /home/user/svnnew_app/trunk/app
Application Trace | Framework Trace | Full Trace
/home/user/.rvm/gems/ruby-1.8.7-p371#app/gems/geokit-1.5.0/lib/geokit/mappable.rb:282:in
`normalize'
/home/user/svnnew_app/trunk/app/vendor/plugins/geokit-rails/lib/geokit-rails/acts_as_mappable.rb:347:in
`normalize_point_to_lat_lng'
/home/user/svnnew_app/trunk/app/vendor/plugins/geokit-rails/lib/geokit-rails/acts_as_mappable.rb:306:in
`extract_origin_from_options'
/home/user/svnnew_app/trunk/app/vendor/plugins/geokit-rails/lib/geokit-rails/acts_as_mappable.rb:203:in
`prepare_for_find_or_count'
/home/user/svnnew_app/trunk/app/vendor/plugins/geokit-rails/lib/geokit-rails/acts_as_mappable.rb:108:in
`find'
/home/user/svnnew_app/trunk/app/app/controllers/client/orders_controller.rb:331:in
`find_notary'
Request
Parameters:
{"id"=>"1198"}
Show session dump Response
Headers:
{"Cache-Control"=>"no-cache", "Content-Type"=>""}
How to resolve this error? Please help me out..

I maintain geokit. I had a look at the source code and that line happens when the response back from the API isn't numeric, e.g. a string but not in the format '99.99, 88.88', so would happen if the geocoding API returned 'NA' or something for example.
And I know StackOverflow says "avoid" the following (clarification, opinion, etc.), but here it is:
I can't help further without you doing a few things:
1) If you could update to the latest geokit (gem: 1.7.1 or the latest github master)
2) Do some debugging, e.g. Open "/home/user/.rvm/gems/ruby-1.8.7-p371#app/gems/geokit-1.5.0/lib/geokit/mappable.rb" (line 282) and least "puts" the string that is causing the error along with any other relevant local variables.
Using gems like pry/pry-debugger are a must for any ruby developer to debugging such issues.
3) Update other software where possible (even upgrade to latest ruby 1.8.7 and rails 2.x)
You're ruby (1.8.7) and rails (2.x) both are quite old and contain various security issues which should be of some concern. I do my best to keep geokit/geokit-rails working with ruby 1.8.7, but even rails 2 compatibility was dropped in geokit-rails, although I'm happy to help because likely you issue is a problem in geokit-rails with supported rails versions.

Related

ArgumentError: Relation#count does not support finder options anymore. Rails 4

I recently upgraded my project from rails3 to rails4. I have below code in my model and it fails in rails 4
test_count = count(:all, :joins => joins, :conditions => conditions, :distinct => true)
The above line of code gives following error.
ArgumentError: Relation#count does not support finder options anymore. Please build a scope and then call count on it or use the activerecord-deprecated_finders gem to enable this functionality.
Any suggestions on how to resolve this error in rails 4?
Thank you
I think your query translates to this:
joins(joins).where(conditions).distinct.count
Have a look at the Rails Guide about the Active Record Query Interface.

Weird Bytes Added to Attribute After Save in Rails

We're experiencing an insane bug where seemingly random bytes are some 90% of the time being tacked on to an email field right at the point when the email is being saved. Here's an example of what might occur:
From params: 'user#example.com'
Before validate: 'user#example.com'
After validate: 'user#example.com'
Before save: 'user#example.com'
Value in object after save: 'user#example.com'
Retrieve record just created by id, and fetch id: 'user#example.com\u007f'
Where the heck did that \u007f (the UTF-8 delete character!!!) come from?! That's by far the most common garbage that shows up. Here's a list some other valid byte sequences that have appeared from time to time:
r\u007f
U\u007f
a\u007f
#m$\u007f
Sometimes I get totally garbage bits, I can't tell if there are more bytes than these due to a PG::CharacterNotInRepertoire error:
0xde 0x4d
0xf6 0x7f
0xbc
0xe3 0x6c 0x24
Given the PG::CharacterNotInRepertoire errors that occur, I'm assuming this is happening somewhere immediately before the value is being saved, but outside of the scope of my application code.
Note that this is strangely not happening for any other fields for the user.
Here are all the callbacks that currently touch the email address:
#strip! and #downcase! before validation
Format validation with the regex \A[A-Za-z0-9._%+-]+#(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,20}\z
Some app info:
Ruby v2.2.0
Rails v4.1.8
Postgres v9.3.2
PG v0.17.1
Turns out that pg-ruby < v0.18.0 is incompatible with Ruby v2.2 despite there being no obvious warnings to the contrary...
https://bitbucket.org/ged/ruby-pg/issue/210/crazy-bytes-being-added-to-record
Upgrade now or get bit(s).
Also be aware that if you're using Rails 4.2.0, there is an issue with pg 0.18.* that affects writing binary data. I currently have 4.2.0 monkey-patched with the the patch that will be in 4.2.1. See https://github.com/rails/rails/pull/17680 for details if you are running 4.2.0 with Ruby 2.2 (and thus with pg 0.18.). There may be similar issues on Rails 4.0. and 4.1.* - I haven't figured out which version have the patch, and whether those versions have been released yet.
My monkey-patch for 4.2.0 looks like:
# Should release in Rails 4.2.1
# PostgreSQL, Fix change detection caused by superfluous bytea unescaping
# See https://github.com/rails/rails/pull/17680
if Rails.version == '4.2.0'
module ActiveRecord
module ConnectionAdapters
module PostgreSQL
module OID # :nodoc:
class Bytea < Type::Binary # :nodoc:
def type_cast_from_database(value)
return if value.nil?
return value.to_s if value.is_a?(Type::Binary::Data)
PGconn.unescape_bytea(super)
end
end
end
end
end
end
end

invalid salt (BCrypt::Errors::InvalidSalt)

Since upgraded to Ruby 2.2.0 I get the following message in my tests:
invalid salt (BCrypt::Errors::InvalidSalt)
I didn't find any upgrade notice helping me to understand the problem. I'm using Rails 4.1.8 and Sorcery 0.8.6.
Anybody else having this problem?
MORE Details:
I'm using Sorcery and not Devise. The encrypted data is the password.
It all started in Cucumber tests, in 2 cases:
When I used to send the #user to the mailer to prepare the data for the mails. Here was the code:
UserMailer.passphrase_reset_notification(#user).deliver
Which generated the exception with the message I wrote in the initial message. As a workaround instead of sending the #user I sent the fields I needed and it worked. Here's the new code:
UserMailer.passphrase_reset_notification(#user.name, #user.email).deliver
But the second case is the sign up. It failed in dev and I had to add :salt to user_params to fix it. But it does not fix the thing in the test env.
There's no stack trace, just that one liner message with the lines of my scenario leading to the error.
And I press "Sign up"
invalid salt (BCrypt::Errors::InvalidSalt)
./app/controllers/users_controller.rb:66:in block in create'
./app/controllers/users_controller.rb:64:increate'
./app/controllers/application_controller.rb:120:in scope_current_tenant'
./features/step_definitions/web_steps.rb:53:in/^(?:|I )press "([^"]*)"$/'
features/users/sign_up.feature:149:in `And I press "Sign up"'
I removed the "null: false" for the field "salt" in the user table, as suggested by a community member in a post on a more or less similar issue, it didn't help either.
My main question is still the same: what the Ruby new version (2.2.0) has to do with this? And what might be the other surprises if I upgrade the prod?
I just fixed this. Turned out it had to do with serializing an object with has_secure_password (which uses bcrypt-ruby)
More specifically, something like the following was causing the issue with Sidekiq as it tried to serialize arguments into objects for Redis queueing.
#user = User.new(
:firstname => 'Scott',
:lastname => 'Klein',
:password => 'mypass',
:password_confirmation => 'mypass'
)
#user.save!
# broken
# note that #user.password can still be called here
# and sidekiq will attempt to serialize this whole object using YAML
# and this is the serialization issue that barfs (in the depths of YAML)
UserMailer.delay.new_user_signup(#user)
# fixed
# i just passed the id and then recalled the user record in the mailer class
UserMailer.delay.new_user_signup(#user.id)
I've had similar problem. Investigation made me conclude that it's bcrypt not playing well with Psych (that's the Ruby system library for generating and parsing YAML).
There's an open bcrypt issue now. Waiting for gem author to fix it.
** FIXED **
The problem, at least mine, is fixed. I just upgraded the bcrypt gem from 3.1.
9 to 3.1.10 and it was it! Thanks Oleg to have created an issue on bcrypt account.

Changes to mass_assignment_authorizer cause errors in Ruby on Rails 3.1

Protecting against mass assignment as in this railscast no longer works in Rails 3.1.
Error given is:
wrong number of arguments (1 for 0)
for
app/models/user.rb:20:in `mass_assignment_authorizer'
If you're trying to implement the override technique in Ryan's Railcasts, but using Rails 3.1.0, then re-writing the private def in the model to:
def mass_assignment_authorizer(role = :default)
super + (accessible || [])
end
I found this cleared the
wrong number of arguments (1 for 0)
error above (ie just adding (role = :default), and also correlates with the answer above
Looking in the source it appears that, at least in master, there is a default option of :default for mass_assignment_authorizer as seen here.
Which version of rails 3.1 are you using?, it may be worth trying it against head by changing your Gemfile:
gem 'rails', :git => 'git#github.com:rails/rails.git'

Geokit and rails 3

I am using the geokit gem and plugin with rails 3. It seems there is a known issue with them, which can be seen here http://github.com/andre/geokit-rails/issues#issue/15
Now, I tried to follow the solution provided at the bottom. I pasted that function definition, at the end of the file, just above acts_as_mapable, and just after the first time it was called, but nothing happened each time.
Any idea what else can be done?
Thanks
I ran into similar problems upgrading my app to rails 3. I'm still using Geokit for geocoding but Active Record scopes for distance based database queries. It's pretty convenient, and you still get all of the Active Record 3 goodness. Here's an example from my User model:
scope :near, lambda{ |*args|
origin = *args.first[:origin]
if (origin).is_a?(Array)
origin_lat, origin_lng = origin
else
origin_lat, origin_lng = origin.lat, origin.lng
end
origin_lat, origin_lng = deg2rad(origin_lat), deg2rad(origin_lng)
within = *args.first[:within]
{
:conditions => %(
(ACOS(COS(#{origin_lat})*COS(#{origin_lng})*COS(RADIANS(users.lat))*COS(RADIANS(users.lng))+
COS(#{origin_lat})*SIN(#{origin_lng})*COS(RADIANS(users.lat))*SIN(RADIANS(users.lng))+
SIN(#{origin_lat})*SIN(RADIANS(users.lat)))*3963) <= #{within}
),
:select => %( users.*,
(ACOS(COS(#{origin_lat})*COS(#{origin_lng})*COS(RADIANS(users.lat))*COS(RADIANS(users.lng))+
COS(#{origin_lat})*SIN(#{origin_lng})*COS(RADIANS(users.lat))*SIN(RADIANS(users.lng))+
SIN(#{origin_lat})*SIN(RADIANS(users.lat)))*3963) AS distance
)
}
}
Here's a blog post with a little more detail on the subject: http://stcorbett.com/code/distance-queries-with-rails-3-without-geokit/
jlecour's port to rails 3 should solve any issues you were having last year.
Make sure you're using mysql or postgres if you're doing distance calculations.
After trouble installing the geokit-rails3 gem on Rails 3.1 I moved to the geocoder gem. It has distance calculation as well (be sure to not forget the s in #your_model.nearby*s*(5)). There is also a Railscast.
Here is port of geokit to rails 3, incomplete through:
https://github.com/jlecour/geokit-rails3
For those still having trouble with geokit, i moved on to using mongodb... which has inbuilt distance search n all...
Hey Amit, Not sure if you sorted this out yet but I'll tell you what I did just in case.
I forked andre's geokit-rails source and then cloned it locally and added the code from this gist at line 34 of lib/geokit-rails/acts-as-mappable.rb, just after the line that reads module ClassMethods # :nodoc:.
Then I commited those changes back to my forked repo on github and used my fork to install the source as a plugin to my rails 3 app. That seemed to work straight away, but make sure you have the acts_as_mappable line added to whatever model you are wanting to do distance calculations on and make sure you have two float columns on that db named :lat and :lng.

Resources