user2784630
user2784630

Reputation: 806

Geocoder: Near method removed from query?

Using Geocoder in Rails 4, I'm running to an issue trying to display the distance on a page. I have the models Kid, Dad and Mom. Kid belongs to a Mom and Dad. Mom's are based on an address or being online.

Dads Controller

def show
  @dad = Dad.find(params[:id])
  near = Mom.near(@user_location, 5000, :select => "kids.*")
  online = Mom.where(is_online: true)    
  near = near.arel.constraints.reduce(:and)
  online = online.arel.constraints.reduce(:and)
  mom = Mom.where((near).or(online))
  @kids = @dad.kids.joins(:mom).merge(mom).order("moms.is_online DESC")
end

Now when I try to use distance in the view:

<% @kids.each do |kid| %>
  <%= kid.mom.distance.round(0) %>
<% end %>

I get the error:

Undefined method `distance' for #<Mom:0x9e7fc94>

After debugging, I found out the distance for Mom isn't working because the near method is gone. I'm not sure what I can do to get it to be apart of the Mom model again. I was looking here: github.com/alexreisner/geocoder/issues/99 and here: github.com/alexreisner/geocoder#known-issue for some possible answers. Any idea on what I should do?

Upvotes: 0

Views: 200

Answers (1)

Dmitry
Dmitry

Reputation: 163

Interesting, maybe it is not the most elegant solution, but what about:

<%= kid.mom.distance_to [kid.latitude, kid.longitude] %>

Upvotes: 1

Related Questions