On Monday, September 9, 2013 12:05:10 PM UTC-7, David Lutterkort wrote:
>
> Hi,
>
> for a simple table like
>
> create_table :nodes do
>   primary_key :id
>   column         :last_checkin, 'timestamp with timezone'
> end
>
> I'd like to use the database's idea of time rather than the server's in 
> the corresponding model class, i.e
>
> class Node < Sequel::Model
>   def checkin
>     # This doesn't work, but I'd like to do something like this so that
>     # ultimately the update query looks like
>     #   update nodes
>     #   set last_checkin = now()
>     #   where id = :some_id
>     self.last_checkin = Sequel.function(:now)  
>     save_changes
>   end
>
> Is something like that possible ?
>

Well, sort of, but not in the way you are doing it.  The issue is that 
model instances are designed to store values, not expressions.  The main 
reason the code given will cause problems is due to the typecasting code, 
since you can't typecast a function expression to a datetime value.  You 
can hack around it doing:

  self.values[:last_checkin] = Sequel.function(:now) # or 
Sequel::CURRENT_TIMESTAMP

The issue with doing this is that if you call self.last_checkin later, you 
are going to get the expression instead of the value.  You'll have to 
refresh the instance before being able to get the value.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to