On Saturday, October 6, 2012 7:04:34 PM UTC+2, Marc Cooper wrote:
>
> I add created_at and updated_at fields to all tables via before_create and 
> before_update on Sequel::Model. Works great.
>

I assume you are using the touch plugin, since Sequel::Model doesn't 
support automated setting of those fields by default.
 

> I now have a many_to_many relationship with its associated join table. 
> Again this all works fine.
>
> However, in the case of the join table, the two *_at fields are not 
> touched.
>
> Is there a way to have these fields touched on creation and update?
>

There is not currently a way to do so automatically in Sequel. 

Currently, the touch plugin doesn't appear to work correctly with 
many_*_many associations, at least on some databases.  I'm considering 
changing the plugin so that it doesn't drop down the the dataset layer for 
updating associations, which it currently does for performance reasons, but 
which causes issues (see issue #566 on GitHub).  Note that that still won't 
do what you want, since it wouldn't update the join table.

Personally, I recommend using database defaults for created_at columns and 
database triggers for updated_at columns, when possible, as they are more 
reliable and have fewer issues once initially setup.

Assuming you can't use a database trigger, the best way to do what you want 
currently is an after_save hook that updates the join table:

  def after_save
    super
    DB[:join_table].where(:some_fk=>pk).update(:updated_at=>Time.now)
  end

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sequel-talk/-/BNR7JPauy4YJ.
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.

Reply via email to