user2492854
user2492854

Reputation: 401

undefined local variable or method in rails

In my rails application I have two tables named coordinates and tweets .To fetch queries the condition is decided in the coordinates controller and executed in the tweets controller. Its basically a tweets search table to fetch matching tweets. . I am using find_by_sql method. My Coordinates_controller

class CoordinatesController < ApplicationController

  def home 
  end
  # def paramas(b)
  #  
  # @b = params[:show]
  # return @b

  #end
  #def coor(latitude,longitude)
  # @latitude=0
  #@longitude=0
  #end

  def query
    a = Coordinates.where(city: params[:show])
    b = a.first
    if a.count == 1
      latitude = b.latitude
      longitude= b.longitude
    end

    if(latitude=0 && longitude=0) then
      sql="Select * from tweets where tweet_text LIKE '%text%' AND user_loc LIKE 'show' order by id desc LIMIT 30"
    else if (latitude!=0 && longitude!=0) 
           min_lat = latitude - 1.0
           max_lat = latitude + 1.0
           min_lng = longitude - 1.0
           max_lng =  longitude + 1.0
           sql = "Select * from tweets where tweet_text LIKE '%text%' AND ( ((longitude BETWEEN min_lng and max_lng) AND (latitude BETWEEN min_lat and max_lat)) OR (user_loc LIKE 'show') ) order by id desc LIMIT 30"
         else
           sql="Select * from  tweets where tweet_text LIKE  '%text%' LIMIT 30"
         end    

    end
  end     
  #end


  #end

My tweets_controller

class TweetsController < ApplicationController

  include CoordinatesHelper
  def search
    render 'tweets/search'
  end


  def index

    # include CoordinatesHelper
   # sql=query
    @tweets=Tweets.find_by_sql(sql)
    #render 'tweets/index'
  end
end

The sql variable comes from the coordinates_controller and is decided by the tweets controller.But for some reasons tweets_controller is not recognizing the sql variable inside the coordinates controller. It says "undefined method or local variable sql".Any help is appreciated

Upvotes: 0

Views: 910

Answers (1)

sawa
sawa

Reputation: 168249

Local variable sql in the CoordinatesController#query definition is not visible to anywhere outside of the definition. Even if you make it visible to instances of CoordinatesController (by turning it into an instance variable), it will not be visible to TweetsController.

Upvotes: 1

Related Questions