@oldbid = 0 why?

Okay I’ve been working on a little biding system. In my model I have
class Item < ActiveRecord::Base
has_many :bids

def highest_bid
@bid = 0
self.bids.each do |bid|
if @bid == nil
@bid = bid.max
else
if bid.max > @bid
@oldbid,@bid = @bid,bid.max
end
end
end
if @oldbid == 0
“#{@bid} - #{@oldbid+1} = #{@bid -= @oldbid+1}”
else
dif = @bid - @oldbid
@bid -= dif
end
end
end

It’s kinda in debug mode as you can see…
But, @oldbid is 0 when there are two bids… I don’t know if it’s
because of the order they come in from SQL or what also that is a flaw
in the system so can I make it return them in a less to higher form?

Okay yes it’s because of their order. How can I fix this?

Hi –

On Thu, 6 Jul 2006, NoA wrote:

if bid.max > @bid

end
end

It’s kinda in debug mode as you can see…
But, @oldbid is 0 when there are two bids… I don’t know if it’s
because of the order they come in from SQL or what also that is a flaw
in the system so can I make it return them in a less to higher form?

I’m afraid I’m not fully following the logic of your highest_bid
method, but you could probably do it more easily like this:

class Item < ActiveRecord::Base
has_many :bids
has_one :highest_bid,
:class_name => “Bid”,
:order => “amount DESC”

(I’m assuming here that “amount” is the column with the bid amount,
but if not, you’d have to change it.)

David


“To fully realize the potential of Rails, it’s crucial that you take
the time to fully understand Ruby–and with “Ruby for Rails” David
has provided just what you need to help you achieve that goal.”
– DAVID HEINEMEIER HANSSON, in the foreword to RUBY FOR RAILS.
Complete foreword & sample chapters at Ruby for Rails!