On Thursday, May 29, 2014 5:37:31 AM UTC-7, Victor 'Zverok' Shepelev wrote:
>
> Hi all.
>
> I have a situation here:
> One of my models (called, basically, Mention) is stored not in one table,
> but in "dumbly sharded" list of tables:
> * mentions of client 1 in table mentions_client_1
> * mentions of client 82 in table mentions_client_82
> ...and so on.
>
> So, class Mention has method Mention.make_class(client), which dynamically
> generates class, descending from Mention, with "right" dataset
> :"mentions_client_#{client.id}".
>
> Surprisingly, everything works pretty well.
>
> But there goes complactions:
> When I generate class based on Mention, I setup some additional
> associations:
>
> one_to_many: :topics,
> class: MentionTopic,
> conditions: {client_id: self.client.id} # should be setup dynamically
> because of this condition
>
> And corresponding association in MentionTopic:
>
> many_to_one :mention,
> dataset: ->{Mention2.make_class(client).dataset.where(id: mention_id)}
>
> End everything still works, hooray!
>
> Now the problem:
>
> MentionTopic.where(client_id: 1).eager(:mention)
> is NOT working: Invalid association topics for Mention
> It seems, here the associated class name is deduced from assocation name,
> not from association dataset model (and Mention indeed have no associations
> :topics, only its descendants have).
>
> How can I fix everything up (without eager everything works fine, but too
> slow, unfortunately)?
> How can I possibly simplify the entire structure? (Sadly, database schema
> redesign is not an option).
>
You can fix things up by writing your own eager loader using the
:eager_loader option, see examples in the Advanced Associations guide.
You may be able to simplify things by using a delayed evaluation for the
table name: Mention.dataset = DB.from(Sequel.delay{...}), but you'll have
to come up with a way to make the delayed evaluation return the correct
name. In a web app, this can usually be done by using a rack middleware,
assuming you can determine the table to use per request.
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.