ActiveRecord Help - Not finding Data

Hello,

I’m farily new to RoR and am having a problem trying to retrieve data
from my database. Here is the code from my controller:

def index
zip = params[“zip”]
@zip1 = Zipcode.find(:first, :conditions => [“zip = ?”, zip])

lat = @zip1.latitude.to_f
long = @zip1.longitude.to_f

radius = 50

@bookstores = Bookstore.find_by_sql ["SELECT * FROM bookstores WHERE
zipcode IN (
SELECT zip
FROM zipcodes
WHERE degrees(acos(
sin( radians(zipcodes.latitude) )

  • sin( radians(?))
  • cos( radians(zipcodes.latitude))
  • cos( radians(?))
  • cos( radians(zipcodes.longitude - ?) )
    ) ) * 69.09 < ?)", lat, lat, long, radius.to_i]
    end

This is a proximity search by zip code. The problem is on the view which
has this code:

<% for bookstore in @bookstores -%>

<%= h(@bookstore.name) %>


<% end %>

Which results in this error message:

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.name

I know the name exists. The find_by_sql works perfectly but when I add
it here, it blows up on me.

Any ideas?

Thank you so very much.

Chris W. wrote:

<% for bookstore in @bookstores -%>

<%= h(@bookstore.name) %>


<% end %>

Which results in this error message:

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.name

The local variable bookstore has a value. The instance variable
@bookstore
does not. Either…

  1. Change <% for bookstore… to <% for @bookstore

or,

  1. Change
    <%=h(@bookstore… to <%=h bookstore…

The variable type/name in the two lines have to match.

hth,
Bill

Bill,

Thank you so very much. That did the trick. What a noob question, eh? :slight_smile:

Thanks again.

Chris W.

Bill W. wrote:

Chris W. wrote:

<% for bookstore in @bookstores -%>

<%= h(@bookstore.name) %>


<% end %>

Which results in this error message:

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.name

The local variable bookstore has a value. The instance variable
@bookstore
does not. Either…

  1. Change <% for bookstore… to <% for @bookstore

or,

  1. Change
    <%=h(@bookstore… to <%=h bookstore…

The variable type/name in the two lines have to match.

hth,
Bill

Chris W. wrote:

Bill,

Thank you so very much. That did the trick.

You’re very welcome.

What a noob question, eh? :slight_smile:

That’s one of the things the list is for! :wink:

Best regards,
Bill