Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6557983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:08:34+00:00 2026-05-25T13:08:34+00:00

I have the model Post: class Post < ActiveRecord::Base has_one :location, :dependent => :destroy

  • 0

I have the model Post:

class Post < ActiveRecord::Base
has_one :location, :dependent => :destroy
belongs_to :person
belongs_to :activity

I have the model Locations:

class Location < ActiveRecord::Base
belongs_to :post
validates :address, :presence => true
attr_accessible :address, :latitude, :longitude
geocoded_by :address
after_validation :geocode, :if => :address_changed?

I need to find all Posts which are in 50 miles from given location. I looked for examples but a did not find what I need. I tried to solve the problem in two ways and failed. I am beginner in Rails and uncounted with the problem, I think that It will be useful for other using has_one model.

I tried:

posts_controller.rb

def index
if params[:saddress].present?
  @locations = Location.near(params[:saddress], 50, :order => :distance)
  for location in @locations
    @posts << location.post
  end
else
  @posts = Post.all
end

index.html.erb

 <h1>Events</h1>
 <fieldset>
 <legend>Find event</legend>
 <%= form_tag(posts_path, :method => "get") do %>
   <%= label_tag(:saddress, "Address:") %>
   <%= text_field_tag :saddress, params[:saddress] %> <br/>
   <%= label_tag(:sactivity, "Activity type:") %>
   <%= select_tag :sactivity, options_from_collection_for_select(Activity.all, "id", "name", params[:sactivity]) %>
   <%= submit_tag "Поиск"%>
 <%end%>
 </fieldset>
 <%if @user%>
   <%= link_to "Новое событие", new_post_path %>
 <%end%>
 <table>
 <tr>
 <th>Created</th>
 <th>Author</th>    
 <th>Event</th>
 <th>Address</th>
 <th>Activity type</th>
 </tr>
 <% for post in @posts.sort.each %>
   <%if post%>
     <tr>
     <td><%= post.created_at %></td>
     <td><%= post.person.name %></td>
     <td><%= link_to post.name, post %></td>
     <td><%if post.location%><%= post.location.address %> <%end%></td>
     <td><%= post.activity.name %></td>
     </tr>
   <%end%>
 <%end%>
 </table>

It resulted in error:

NoMethodError in PostsController#index
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.<<

Please help me what is wrong. Maybe there is some other easy method for this.

Also I tried this one in posts_controller.rb:

if params[:saddress].present?
  @locations = Location.near(params[:saddress], 50, :order => :distance)
  @posts = Post.find(:all, :include => [:location], :conditions => ["locations.id in ?", @locations])
else
  @posts = Post.all
end

In this case I had problem with SQL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:08:34+00:00Added an answer on May 25, 2026 at 1:08 pm

    In your PostsController#index method, you need to initialize the @posts variable. to be an array before you can append items to it. If you ommit that (like you did), @posts is going to be implicitly initialized to nil which explains your error. Change your code like this:

    def index
      if params[:saddress].present?
      @posts = []
        @locations = Location.near(params[:saddress], 50, :order => :distance)
        for location in @locations
          @posts << location.post
        end
      else
        @posts = Post.all
      end
    end
    

    Another (shorter) variant of the above code is

    def index
      if params[:saddress].present?
        @locations = Location.near(params[:saddress], 50, :order => :distance)
        @posts = @locations.collect(&:post)
        # ^^ this is the short form of the following equivalent expression:
        # @posts = @locations.collect{ |loc| loc.post }
      else
        @posts = Post.all
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, Article and Post that both inherit from a base model
I have 3 model, User, Post, Comment with definition like below class Post <
I have the following models: class Post(models.Model): message = models.TextField() (etc.) class UserProfile(models.Model): user
i have this in my BlogRepository public IQueryable<Subnus.MVC.Data.Model.Post> GetPosts() { var query = from
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
I have the following model and url routes. There is one Post model that
Alright, I have a fairly simple design. class Update(models.Model): pub_date = models.DateField() title =
I have a model with a reference property, eg: class Data(db.Model): x = db.IntegerProperty()
In my rails app I have 2 models: post and post_translations. class PostTranslation <
I have a model being populated by my data layer and then I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.