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 -%> <h3><%= h(@bookstore.name) %></h3> <% 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. -- Posted via http://www.ruby-forum.com/.
Chris Williams wrote:> > <% for bookstore in @bookstores -%> > <h3><%= h(@bookstore.name) %></h3> > <% end %> > > Which results in this error message: > > You have a nil object when you didn''t expect it! > The error occured while evaluating nil.nameThe local variable bookstore has a value. The instance variable @bookstore does not. Either... 1) Change <% for bookstore... to <% for @bookstore... or, 2) 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? :) Thanks again. Chris Williams Bill Walton wrote:> Chris Williams wrote: >> >> <% for bookstore in @bookstores -%> >> <h3><%= h(@bookstore.name) %></h3> >> <% 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, > > 2) Change > <%=h(@bookstore... to <%=h bookstore... > > The variable type/name in the two lines have to match. > > > hth, > Bill-- Posted via http://www.ruby-forum.com/.