Dakshata Gulkhobare
2010-Aug-27 12:50 UTC
Sort table by price and print the cheapest price car
Hello, I am quite new to ruby. I have coded in Java earlier and the code to fire a query in ruby is a lot different and I am struggling. I have this model create_table "used_cars", :force => true do |t| t.string "make" t.string "model" t.string "trim" t.decimal "engine_size_cc" t.string "bodytype" t.decimal "doors" t.string "fuel_type" t.string "car_id" t.string "colour" t.decimal "price" t.string "dealer" t.string "town" t.string "cap_car_id" t.string "registration_no" t.date "reg_year" t.string "inc_options" t.string "url" t.datetime "created_at" t.datetime "updated_at" end I need to generate an ad for the cheapest car..I know what query to fire, but dont know what ruby syntax to use..I want to order it by price...and then use different fields to print the ad. I tried few things like car_price = UsedCar.find_by_sql"SELECT price FROM used_cars" or car_price = UsedCar.find_by_price(true,true) Also it will be really great if anybody can direct me to right study material. I am reading agile web dev with rails and ruby cookbook. But need to work hard on DB syntax. Can anybody please help! Thanks in advance :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dakshata Gulkhobare
2010-Aug-27 12:51 UTC
Re: Sort table by price and print the cheapest price car
Also I have used :order and :order_by but no luc k so far :( Dakshata -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dakshata Gulkhobare
2010-Aug-27 14:19 UTC
Re: Sort table by price and print the cheapest price car
car_price = UsedCar.find(:all, :order => ''price DESC'' ) puts "#{car_price[0]}" I used the above syntax and it o/p s it as merjis-mac-mini:seed_trademark dakshata$ rake db:seed (in /Users/dakshata/source/learning-ruby/seed_trademark) #<UsedCar:0x1a08628> Any ideas??? Thanks Dakshata -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Aug-27 14:28 UTC
Re: Re: Sort table by price and print the cheapest price car
On 27 August 2010 15:19, Dakshata Gulkhobare <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> car_price = UsedCar.find(:all, :order => ''price DESC'' )That will give you them all in descending price order so the first one will be the most expensive. You could have called it cars rather than car_price to indicate that it is an array (or similar) of Cars. If you want them in price increasing you can say cars = UsedCar.find(:all, :order => :price) or to get just the cheapest cheapest_car = UsedCar.find(:all, :order => :price).first> > puts "#{car_price[0]}"puts car_price[0] would have done the same thing.> > > I used the above syntax and it o/p s it as > > > merjis-mac-mini:seed_trademark dakshata$ rake db:seed > (in /Users/dakshata/source/learning-ruby/seed_trademark) > #<UsedCar:0x1a08628>If you want to see the individual fields then you need cheapest_car.make and so on. Why are you doing it in db:seed by the way? Have a look at the Rails Guides at http://guides.rubyonrails.org/. They are very useful. If you are following any tutorials make sure they are for Rails 2.3 assuming that is what you are using. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Aug-27 15:31 UTC
Re: Re: Sort table by price and print the cheapest price car
Colin Law wrote:> On 27 August 2010 15:19, Dakshata Gulkhobare <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > wrote: >> car_price = UsedCar.find(:all, :order => ''price DESC'' ) > > That will give you them all in descending price order so the first one > will be the most expensive. You could have called it cars rather than > car_price to indicate that it is an array (or similar) of Cars. If > you want them in price increasing you can say > > cars = UsedCar.find(:all, :order => :price) > or to get just the cheapest > cheapest_car = UsedCar.find(:all, :order => :price).firstThat will still retrieve all the records. You want find :first, not find :all. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Aug-27 19:50 UTC
Re: Re: Re: Sort table by price and print the cheapest price car
On 27 August 2010 16:31, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote: >> On 27 August 2010 15:19, Dakshata Gulkhobare <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> >> wrote: >>> car_price = UsedCar.find(:all, :order => ''price DESC'' ) >> >> That will give you them all in descending price order so the first one >> will be the most expensive. You could have called it cars rather than >> car_price to indicate that it is an array (or similar) of Cars. If >> you want them in price increasing you can say >> >> cars = UsedCar.find(:all, :order => :price) >> or to get just the cheapest >> cheapest_car = UsedCar.find(:all, :order => :price).first > > That will still retrieve all the records. You want find :first, not > find :all.Yes of course, my mistake. Though in fact rails could be clever enough to work out from the statement find(:all).first that it only needs to fetch the first one. There is a limit to the magic it can sensibly perform however, and I was trying to push it beyond that limit. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Aug-27 20:02 UTC
Re: Re: Re: Sort table by price and print the cheapest price car
Colin Law wrote:> On 27 August 2010 16:31, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > wrote: >>> cars = UsedCar.find(:all, :order => :price) >>> or to get just the cheapest >>> cheapest_car = UsedCar.find(:all, :order => :price).first >> >> That will still retrieve all the records. �You want find :first, not >> find :all. > > Yes of course, my mistake. Though in fact rails could be clever > enough to work out from the statement find(:all).first that it only > needs to fetch the first one.I have the impression that Arel may do things like that, but 2.x ActiveRecord does not.> There is a limit to the magic it can > sensibly perform however, and I was trying to push it beyond that > limit.:)> > ColinBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dakshata Gulkhobare
2010-Aug-29 21:18 UTC
Re: Re: Re: Sort table by price and print the cheapest price car
Thank you Colin, Thank you Marnen for your reply, @Colin: I was using seed because I had just finished populating the DB and was just trying to write the program in the seed to make running the program easier, nothing particular. Now I have written and called the method somewhere else. The cheapest car thing works. Something else has arisen! I have the code on the office machine. will post it on weekdays! Thanks a lot for your help. Colin Law wrote:> On 27 August 2010 16:31, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > wrote: >>> cars = UsedCar.find(:all, :order => :price) >>> or to get just the cheapest >>> cheapest_car = UsedCar.find(:all, :order => :price).first >> >> That will still retrieve all the records. �You want find :first, not >> find :all. > > Yes of course, my mistake. Though in fact rails could be clever > enough to work out from the statement find(:all).first that it only > needs to fetch the first one. There is a limit to the magic it can > sensibly perform however, and I was trying to push it beyond that > limit. > > Colin-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.