On Thursday, August 20, 2015 at 8:58:14 AM UTC-7, Paul Cowan wrote:
>
> I have the following migration which is migrating data from an old table
> to a new table named lists:
>
> self[:lists].insert([:name, :item_name, :type, :account_id,
> :old_tag_id],
> self[:tags].select(:name, :name, "contacts",
> :account_id, :id))
>
> I also want to add 2 rows into another table for every row that is
> inserted into the lists table in this migration.
>
> The List model class from the self[:lists] of the above insert has the
> following after_model hook
>
>
> def after_create
> super
>
> ListStatus.create(name: 'active', position: 1, list: self)
> ListStatus.create(name: 'inactive', position: 2, list: self)
> end
>
> This after_create hook is obviously not ran when records are inserted via
> a migration.
>
> How can I best insert the 2 records in a migration which does not involve
> using the List model object?
>
This seems the easiest to me:
self[:lists].insert([:name, :item_name, :type, :account_id,
:old_tag_id],
self[:tags].select(:name, :name, "contacts", :account_id, :id))
self[:list_statuses].insert([:name, :position, :list_id],
self[:lists].select('active', 1, :id))
self[:list_statuses].insert([:name, :position, :list_id],
self[:lists].select('inactive', 2, :id))
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 http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.