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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/941eb57a-5026-4700-a480-0473bbfc7a79%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.