On Oct 9, 10:01 am, Florent <[email protected]> wrote:
> Sorry for this noob question but I can't get it work after few hours
> on it, it's getting me crazy...
>
> the long story is:
> I have a model with subset methods that use a dynamic argument based
> on Time.now
>
> subset(:recent) { |p| (!p.published) | (p.published < Time.now - 3600
> * 24) }
>
> First thing I spotted: I need to use this syntax in order that
> Time.now is called each time I call MyModel.
> this line:
>
> subset(:tt, :published < Time.now - 3600 * 24)
>
> doesn't work, Time.now is called once when loading my model and keep
> this value each call to the subset.
You should be able to use the following syntax now:
subset(:tt, :published < Sequel::CURRENT_TIMESTAMP - 3600 * 24)
Whether that will work depends on the how the database handles
subtracting a numeric value from a timestamp.
> But what I totally don't understand, it's how OR / AND and NULL values
> is used
> I tried:
>
> >> News.filter { (!published ) | (published < Time.now - 3600 * 24}
>
> => #<Sequel::Postgres::Dataset: "SELECT * FROM \"news\" WHERE true">
published is going to return a Sequel::SQL::Identifier.
!published is going to return false.
(published < Time.now - 3600 * 24) is going to return a
Sequel::SQL::BooleanExpression.
false | Sequel::SQL::BooleanExpression is going to return true, which
is why you get those results.
> >> News.filter { (~published ) | (published < Time.now - 3600 * 24}
>
> => #<Sequel::Postgres::Dataset: "SELECT * FROM \"news\" WHERE (NOT
> \"published \" OR (\"published \" < '2009-10-09
> 18:56:29.704914+0200'))">
This isn't what you want, because published isn't boolean, it's a
timestamp.
> >> News.filter { (published = nil) | (published < Time.now - 3600 * 24}
>
> NoMethodError: undefined method `<' for nil:NilClass
Here you are setting a local variable published to nil, and then
referencing it later. NilClass#< is not defined, which is why you get
the no message error.
> >> News.filter { |o| (o.published = nil.sql_expr) | (o.published < Time.now
> >> - 3600 * 24) }
>
> NoMethodError: undefined method `sql_expr' for nil:NilClass
NilClass#sql_expr is not defined. Even if you include the sql_expr
extension, o.published = nil.sql_expr is just going to return
nil.sql_expr, so you'd end up with:
SELECT * FROM `news` WHERE (NULL OR (`published` < '2009-10-08
13:17:39.387322-0700'))
You need to use a hash if you want IS NULL.
> I just want to filter all NULL values OR rows for 1 day.
> I tried to combine filter with exclude but I don't want a NOT I would
> like a IS NULL
News.filter{{published => nil} | (published < Time.now - 3600 * 24}
Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---