# Sort table by price and print the cheapest price car

**URL:** <https://www.ruby-forum.com/t/sort-table-by-price-and-print-the-cheapest-price-car/193982>\
**Category:** Rails\
**Created:** [August 27, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/sort-table-by-price-and-print-the-cheapest-price-car/193982 "2010-08-27T14:50:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dakshata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dakshata](https://www.ruby-forum.com/u/dakshata)\
**Post date:** [August 27, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/sort-table-by-price-and-print-the-cheapest-price-car/193982/1 "2010-08-27T14:50:16Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![dakshata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dakshata](https://www.ruby-forum.com/u/dakshata)\
**Post date:** [August 27, 2010, 2:51pm UTC](https://www.ruby-forum.com/t/sort-table-by-price-and-print-the-cheapest-price-car/193982/2 "2010-08-27T14:51:47Z")

</div>

Also I have used :order and :order\_by

but no luc k so far ☹

Dakshata

---

<div class="post-metadata">

**Author:** ![dakshata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dakshata](https://www.ruby-forum.com/u/dakshata)\
**Post date:** [August 27, 2010, 4:19pm UTC](https://www.ruby-forum.com/t/sort-table-by-price-and-print-the-cheapest-price-car/193982/3 "2010-08-27T16:19:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dakshata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dakshata](https://www.ruby-forum.com/u/dakshata)\
**Post date:** [August 27, 2010, 5:31pm UTC](https://www.ruby-forum.com/t/sort-table-by-price-and-print-the-cheapest-price-car/193982/4 "2010-08-27T17:31:51Z")

</div>

Colin L. wrote:

> On 27 August 2010 15:19, Dakshata G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![dakshata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dakshata](https://www.ruby-forum.com/u/dakshata)\
**Post date:** [August 27, 2010, 9:57pm UTC](https://www.ruby-forum.com/t/sort-table-by-price-and-print-the-cheapest-price-car/193982/5 "2010-08-27T21:57:07Z")

</div>

On 27 August 2010 16:31, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![dakshata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dakshata](https://www.ruby-forum.com/u/dakshata)\
**Post date:** [August 27, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/sort-table-by-price-and-print-the-cheapest-price-car/193982/6 "2010-08-27T16:39:45Z")

</div>

On 27 August 2010 15:19, Dakshata G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](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

---

<div class="post-metadata">

**Author:** ![dakshata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dakshata](https://www.ruby-forum.com/u/dakshata)\
**Post date:** [August 27, 2010, 10:02pm UTC](https://www.ruby-forum.com/t/sort-table-by-price-and-print-the-cheapest-price-car/193982/7 "2010-08-27T22:02:51Z")

</div>

Colin L. wrote:

> On 27 August 2010 16:31, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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.

🙂

> Colin

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![dakshata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dakshata](https://www.ruby-forum.com/u/dakshata)\
**Post date:** [August 29, 2010, 11:18pm UTC](https://www.ruby-forum.com/t/sort-table-by-price-and-print-the-cheapest-price-car/193982/8 "2010-08-29T23:18:07Z")

</div>

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 L. wrote:

> On 27 August 2010 16:31, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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
