how to achieve in rails way.?

24 views
Skip to first unread message

Darshan d

unread,
Jul 9, 2014, 3:06:53 AM7/9/14
to rubyonra...@googlegroups.com
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.


Colin Law

unread,
Jul 10, 2014, 4:44:03 AM7/10/14
to rubyonra...@googlegroups.com
On 9 July 2014 08:06, Darshan d <darsh...@onze.in> wrote:
> Hi all,
>
> i am a new rubiest. Started exploring rails recently.

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
Reply all
Reply to author
Forward
0 new messages