Geocoder Gem: How to use between two different models?

风格不统一 提交于 2019-12-25 12:30:06

问题


I'm using the Geocoder and Will Paginate gems to sort my results based on the distance from a user's location and a kids' mom's address. My models are like this:

class User
 has_many :kids
 geocoded_by :address
end

class Kid
  belongs_to :mom
  belongs_to :dad
end

class Mom
  has_many :kids
  has_many :dads, through: :kids
  geocoded_by :address
end

class Dad
  has_many :kids
  has_many :moms, through: :kids
end

Now, I'm trying to use the correct scope that I will use the Users.address and compare it to the Kids.moms.addresses

def show
 @dad = Dad.find(params[:id])
 @user_location = current_user.address
 @kids = @dad.kids.near(@user_location, 100).paginate(page: params[:page], per_page: 25).order(created_at: :desc)
end

This gives me the error:

NoMethodError - undefined method `address' for nil:NilClass:
  app/controllers/dads_controller.rb:14:in `show'
  actionpack (4.0.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.0.2) lib/abstract_controller/base.rb:189:in `process_action'
  actionpack (4.0.2) lib/action_controller/metal/rendering.rb:10:in `process_action'
.......

Any suggestions?


UPDATE

So far I've done this now:

class ApplicationController
  before_filter :set_user_location

  private

  def set_user_location
    if signed_in?
      @user_location = current_user.address
    end
  end
end

class DadsController

def show
  @dad = Dad.find(params[:id])
  @kids = @dad.kids.joins(:mom).merge(Mom.near(@user_location, 100)).order(name: :asc, created_at: :desc).paginate(page: params[:page], per_page: 25)
end

The code above gets the current users address to work (using Devise) but now I get the error:

ActiveModel::MissingAttributeError

missing attribute: birthday

For all of my Kids attributes on the page.

<% @kids.each do |kid| %>
   <%= kid.birthday %>
   <%= kid.name %>
   <% if kid.upkeep.present? %>
     <%= kid.upkeep %>
     <%= kid.upkeep_size %> 
   <% end %>      
   <%= kid.age %>
   <%= kid.favorite_color %>
<% end %>

I think this is because it thinks Mom has the same attributes as the kid. Any ideas?


回答1:


This is how you can do it with Geocoder:

def show
  @dad = Dad.find(params[:id])
    near = Mom.near(@user_location, 100, :select => "kids.*")
  @kids = @dad.kids.joins(:mom).merge(near).order(name: :asc, created_at: :desc).page(params[:page])
end

The :select option is what I needed to add.




回答2:


The problem is that Kids aren't geocoded themselves, so you can't use the near scope.

Something like this should get you close:

@dad.kids.joins(:mom).merge(Mom.near("San Francisco, CA, US")).paginate(...).order(...)

Docs

EDIT

Based on your updated question, it looks like current_user is nil. Once you fix that and combine it with the query above, you should be good.



来源:https://stackoverflow.com/questions/22947164/geocoder-gem-how-to-use-between-two-different-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!