On Tuesday, May 2, 2017 at 7:11:32 AM UTC-7, Richard Huang wrote:
>
> Hi,
>
> I'm trying to create a many to many relationship in sequel and I want to 
> add created_at in the middle table, like
>
>
> class User < Sequel::Model
>   plugin :timestamps, update_on_create: true
>
>
>   has_many :roles
> end
>
>
> class Role < Sequel::Model
>   plugin :timestamps, update_on_create: true
>
>
>   has_many :users
> end
>
>
> class RolesUser < Sequel::Model
>   plugin :timestamps, update_on_create: true
> end
>
>
> I added both created_at and updated_at columns to users, roles and 
> roles_users table, but when I call
>
> user.add_role(role) or role.add_user(user), it won't update created_at and 
> updated_at columns for roles_users table, how could I make timestamps 
> plugin work for many to many middle table?
>

You can setup one_to_many associations to the join table model, and use the 
modification methods for that:

  user.add_role_user(:role_id=>role.id)
  role.add_role_user(:user_id=>user.id)

However, the better way is using a database trigger for to handle the 
created_at/updated_at columns, instead of the timestamps plugin.  For 
PostgreSQL, there 
is https://github.com/jeremyevans/sequel_postgresql_triggers

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 https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to