Hi,

We are trying to set nested attributes on an STI model. This is relatively 
straightforward for database columns, as you'd expect, but breaks down once 
we get to associations. We have an STI child that has an association that 
the STI parent (or any of the other children) doesn't have.

Specifically, consider this 
example: https://gist.github.com/benhutton/4ff4d8a98a8345a7bc1cfd7f0b0415b0

A resource has many blocks. One particular type of blocks has one video.

Thus I'd like to do something like Resource.new(block_attributes: [ { type: 
'Blocks::Video', video_attributes: { youtube_id: '12345678' } } ]).

The problem is this 
line: 
https://github.com/jeremyevans/sequel/blob/5.17.0/lib/sequel/plugins/nested_attributes.rb#L174.
 
It creates based on the reflection associated class. I'd like a way to 
override it.

Currently, I am re-defining #nested_attributes_create in my Resource class, 
changing that one line to this:

obj = if reflection.associated_class == Blocks::Base
        attributes[:type].constantize.new
      else
        reflection.associated_class.new
      end

Unfortunately, that's a rather ugly hack — I've got to copy over a lot of 
other code from the gem into my codebase, which of course makes it very 
brittle.

So, two questions:

1. Is there a better way to do this (other than abandoning STI or trying to 
separate this out into multiple associations)?
2. Is this change or something similar something that Sequel would consider 
merging in?

Thanks!

-Ben

-- 
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