On Monday, April 2, 2018 at 2:35:43 AM UTC-7, Андрей Аладьев wrote:
>
> Hello. I am going to store association created/updated dates.
>
> class A
>   # any association with join table
>   many_to_many :bs, :join_table => :a_with_bs
> end
> class B
>   many_to_many :as, :join_table => :a_with_bs
> end
> class AWithB
>   plugin :timestamps
> end
>
> a.add_b b
>
> It will just insert foreign keys
> INSERT into a_with_bs (a_id, b_id) ...
>
> I've found a method *def_many_to_many* in *model/associations.rb*. It 
> uses:
>
> def _join_table_dataset(opts)
>   ds = model.db.from(opts.join_table_source)
>   opts[:join_table_block] ? opts[:join_table_block].call(ds) : ds
> end
>
> It is not able to call *before_update* or *before_validation* of AWithB. 
> Do you plan to support timestamps in join table model? Now we can use 
> postgresql triggers instead. Thank you.
>

many_to_many does not use a model for the join table, so it won't do 
validations, hooks, etc..  You should use one_to_many instead if you want 
model methods to be called in the association modification methods:

A.one_to_many :a_with_bs, :key=>:a_id
a.add_a_with_b(:b_id=>b.id)

However, if you can use database triggers, that is usually a better way to 
go.

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 post to this group, send email to sequel-talk@googlegroups.com.
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