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 :slight_smile:

Also I have used :order and :order_by

but no luc k so far :frowning:

Dakshata

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

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

Colin Law wrote:

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

Colin Law 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.

:slight_smile:

Colin

Best,

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: