Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
join table hooks
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Marc Cooper  
View profile  
 More options Oct 6 2012, 1:04 pm
From: Marc Cooper <auxb...@gmail.com>
Date: Sat, 6 Oct 2012 10:04:34 -0700 (PDT)
Local: Sat, Oct 6 2012 1:04 pm
Subject: join table hooks

I add created_at and updated_at fields to all tables via before_create and
before_update on Sequel::Model. Works great.

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?

Thanks.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Evans  
View profile  
 More options Oct 8 2012, 8:29 am
From: Jeremy Evans <jeremyeva...@gmail.com>
Date: Mon, 8 Oct 2012 05:29:52 -0700 (PDT)
Local: Mon, Oct 8 2012 8:29 am
Subject: Re: join table hooks

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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marc Cooper  
View profile  
 More options Oct 14 2012, 6:42 am
From: Marc Cooper <auxb...@gmail.com>
Date: Sun, 14 Oct 2012 03:42:26 -0700 (PDT)
Local: Sun, Oct 14 2012 6:42 am
Subject: Re: join table hooks

Thanks for that.  Your trigger suggestions is something I'll think about.

Actually, I'm doing the following at the moment:

class Sequel::Model

  def before_create
    return false if super == false
    now = Time.now.utc
    self.updated_at = now
    self.created_at = now
  end

  def before_update
    return false if super == false
    self.updated_at = Time.now.utc
  end

end

Being part of Sequel::Model, I expected it to work on the join table, as it
does elsewhere.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Evans  
View profile  
 More options Oct 16 2012, 9:53 pm
From: Jeremy Evans <jeremyeva...@gmail.com>
Date: Tue, 16 Oct 2012 18:53:43 -0700 (PDT)
Local: Tues, Oct 16 2012 9:53 pm
Subject: Re: join table hooks

The join table in a many-to-many association is not a model table.  If you
want similar behavior, you'll have to use a model for the join table and
one_to_many association to the join table model.

Jeremy


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marc Cooper  
View profile  
 More options Oct 18 2012, 1:17 pm
From: Marc Cooper <auxb...@gmail.com>
Date: Thu, 18 Oct 2012 10:17:35 -0700 (PDT)
Local: Thurs, Oct 18 2012 1:17 pm
Subject: Re: join table hooks

I thought as much. Thanks, as always, for your assistance and advice, it
very much appreciated.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »