how to achieve in rails way.?

Hi all,

i am a new rubiest. Started exploring rails recently.

Here is my problem

i am trying to get objects from my custom database and render them as json objects.

This is how tried. In my controller.

def near
begin
@lat = params[:lat]
@lon = params[:lon]
@near_ids = []

    def get_id(dist_var)
      @near_ids = Brand.find_by_sql("#Long query to find nearest stores around the given latlon  ")  
      puts 'Total id :'+@near_ids.length.to_s
      puts 'distance :'+dist_var.to_s
    end

  get_id(dist = 1)

    while(@near_ids.length < 10)
      dist +=5
      get_id(dist)
    end

      @stores = []
      @near_ids.each do |store|
        @stores << Store.find_by_id(store["id"])
      end
      @user = @stores.paginate(:page => params[:page], :per_page => 10 )
      rescue ActiveRecord::RecordNotFound
      render json: 'Not found', status: 404
  end
end

``

But i know it is not the right way to do it in rails.

how can i achieve this in rails way.? Mean using models how can i query my database to return the needed objects to my controller.?

i use jbuilder to render returned id’s as json.

In that case work right through a good tutorial such as
railsturial.org (which is free to use online) which will show you the
basics of Rails.

Colin