简体   繁体   中英

Ruby on Rails Geocoder Gem. List results of nearby search returning confusing data

I am trying to build an app with Rails that allows users to search for job listings using geographic locations (addresses to be specific). I'm using the 'geocoder' gem.

gem 'geocoder'

I can successfully search using the rails console using Job.near('sydney',10) and it returns a listing from my database in an array.

2.2.2 :001 > Job.near('sydney', 10 )
  Job Load (97.5ms)  SELECT jobs.*, 6371.0 * 2 * ASIN(SQRT(POWER(SIN((-33.8688197 - jobs.latitude) * PI() / 180 / 2), 2) + COS(-33.8688197 * PI() / 180) * COS(jobs.latitude * PI() / 180) * POWER(SIN((151.2092955 - jobs.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((jobs.longitude - 151.2092955) / 57.2957795), ((jobs.latitude - -33.8688197) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "jobs" WHERE (jobs.latitude BETWEEN -33.958751860591875 AND -33.77888753940813 AND jobs.longitude BETWEEN 151.10098469477293 AND 151.31760630522706 AND (6371.0 * 2 * ASIN(SQRT(POWER(SIN((-33.8688197 - jobs.latitude) * PI() / 180 / 2), 2) + COS(-33.8688197 * PI() / 180) * COS(jobs.latitude * PI() / 180) * POWER(SIN((151.2092955 - jobs.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 10)  ORDER BY distance ASC
 => #<ActiveRecord::Relation [#<Job id: 73, title: "Dog walkingn", description: "walk my dog everyday", price: "$25", user_id: "24", timeframe: "daily", created_at: "2016-07-29 05:50:00", updated_at: "2016-07-29 05:50:00", location: nil, job_type_id: "3", street_number: "100", street: "Pitt Street", suburb: "Sydney", state: "NSW", postcode: "2000", country: "Australia", longitude: 151.2092955, latitude: -33.8688197>]>

But when I do this in controller and try and send the data to the view (jobs.html.erb), all I get is :

#<Job::ActiveRecord_Relation:0x007fd66475abd8>

here is my controller code:

def location

if params[:location].present?
  jobs = Job.near(params[:location],
   params[:proximity])

  @jobs = jobs
else
  @noJobs = 'there is nothing nearby'
  @jobs = nil
end

end

and my view only consists of: (jobs.html.erb)

 <%=  @jobs %>       

if i run

<%=@jobs[0].title%>

in the view it just returns

ActiveRecord::StatementInvalid in Jobs#location

can anyone explain to me what is going on and how i can access the data? and what the active record relation actually means?

I'm still pretty new to coding, and this has completely stumped me. I cant find the answer anywhere. Really appreciate any help, Thanks!

As you can see, @jobs is a ActiveRecord::Relation object, which is essentially an Array of ActiveRecord objects. To display them in the view, you will have to use some sort of a loop, most likely an each :

<% @jobs.each do |job| %>
  <%= job %>
<% end %>

Also, <%= job %> alone might not do the trick. It will return something like a object with it's id. You will need to call job.title or some other attribute/method on the object, so you get the actual value in the view:

<% @jobs.each do |job| %>
  <%= job.title %>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM