Forum: Ruby on Rails testing for nil numeric value in find

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James B. Byrne (Guest)
on 2006-01-27 22:47
(Received via mailing list)
In the agile/rails tutorial book the following construct is added
to products.rb.

  def self.salable_items
    find( :all,
          :conditions   =>  "date_available   <=  now()",
          :order        =>  "date_available desc")
  end


Now, I wish to extend :conditions => to exclude products that have
a nil value for the price.  I have tried different syntax using
"price.nil?", "not", and "!", but have not hit upon the right
combination.  Can someone show me the way?

Regards,
Jim

--
     *** e-mail is not a secure channel ***
mailto:byrnejb.<token>@harte-lyne.ca
James B. Byrne                Harte & Lyne Limited
vox: +1 905 561 1241          9 Brockley Drive
fax: +1 905 561 0757          Hamilton, Ontario
<token> = hal                 Canada L8E 3C3
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-01-27 22:54
(Received via mailing list)
Hi,
I haven't tested this, but I think it should work.

def self.salable_items
    find( :all,
          :conditions   =>  ["(date_available   <=  ?) AND (price <>
|NULL|)",Time.now()],
          :order        =>  "date_available desc")
  end

Eric



James B. Byrne wrote:
> Now, I wish to extend :conditions => to exclude products that have
> James B. Byrne                Harte & Lyne Limited
> vox: +1 905 561 1241          9 Brockley Drive
> fax: +1 905 561 0757          Hamilton, Ontario
> <token> = hal                 Canada L8E 3C3
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Eric Goodwin
http://www.ericgoodwin.com
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-01-27 22:57
(Received via mailing list)
Disregard the pipes in the statement, I think my editor added them in by
accident.

Eric Goodwin wrote:
> Eric
>>           :order        =>  "date_available desc")
>>
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
>


--
Eric Goodwin
http://www.ericgoodwin.com
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-01-27 22:57
(Received via mailing list)
def self.salable_items
     find( :all,
           :conditions   =>  "date_available   <=  now() && price > 0",
           :order        =>  "date_available desc")
end

Since price is defined as not null and a decimal, its default value will
be
0.00 (I think). Adding an SQL check for price > 0 will give what you
want.


Bob Silva
http://www.railtie.net/
This topic is locked and can not be replied to.