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
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:> 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
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Eric Goodwin
http://www.ericgoodwin.com
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/
> -----Original Message-----
> From: rails-bounces@lists.rubyonrails.org [mailto:rails-
> bounces@lists.rubyonrails.org] On Behalf Of James B. Byrne
> Sent: Friday, January 27, 2006 1:47 PM
> To: rails@lists.rubyonrails.org
> Subject: [Rails] testing for nil numeric value in find
>
> 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
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
Disregard the pipes in the statement, I think my editor added them in by accident. Eric Goodwin wrote:> 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: >> 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 >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > >-- Eric Goodwin http://www.ericgoodwin.com
On Fri, 27 Jan 2006 13:57:08 -0800, "Bob Silva" <me@bobsilva.com> wrote:> 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.In this case I have allowed price to remain null, which is why I wish to exclude it from salable_items. In other words, I wish to both provide for freebies (0.00) and exclude cases where the product has not yet had a price set (nil). It is a variant on the basic setup, which I already have working. 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