On Wed, May 31, 2023 at 8:44 AM Esteban <evan.richa...@safehub.io> wrote:

> Howdy! Huge fan of the forum, and all the work you do Jeremy.
>
> I'm using the timestamps plugin, and I discovered that created_at isn't
> set for join tables.


This is expected, as the plugin only handles timestamps for the current
model, not the timestamps for any associated models or join tables.


> I've got two models (Group and User) connected via many_to_many, with a
> typical join table groups_users.
>
> It turns out my app hasn't been tracking when the associations were
> created, even with the timestamps plugin applied to the join table's model.
>
> In order to get the timestamp plugin to run, I've found I need to manually
> insert the rows into the join table like so:
>
> ```
>   many_to_many :users, left_key: :group_id, right_key: :user_id, adder:
> (lambda do |user|
>     GroupsUser.create(user_id: user.id, group_id: id)
>   end)
> ```
>
> Here is a minimally reproducible example
> <https://gist.github.com/lambwaves/521cca5193e2cafd17057e7b76d25ef3>,
> which sets up the tables.
>
> Is my solution the best way to track created_at for a join table in sequel?
>

For create timestamps, by far the best way would be a column default of
CURRENT_TIMESTAMP.  That way you don't need to do anything at the
application level to handle the timestamp.  I don't think you would need to
worry about update timestamps for a join table, since in general a join
table should be inserted into and deleted from, but not updated.

If you don't want to modify the database to set a column default of
CURRENT_TIMESTAMP for the join table's created_at column, then your
approach seems fine.

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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSScUAF2%3DPzhrMFBTS9apoX-NXuekOBo%3DrDWupn%2B34phzdw%40mail.gmail.com.

Reply via email to