On Tuesday, January 5, 2016 at 9:37:04 AM UTC-8, [email protected] wrote:
>
> I have three models:
>
> Video
>
> VideoKeyword which has video_id and keyword_id associated to Video and 
> Keyword respectively
>
> Keyword
>
> In my Video model, I have:
>
>   one_to_many :related_videos, class: self, dataset: proc { |r|
>     kids = keywords_dataset.select(:keyword_id)
>     vids = VideoKeyword.distinct(:video_id).
>       select(:video_id).
>       where(keyword_id: kids)
>     Video.where(id: vids)
>   }
>
>
>   one_to_many :keywords, class: 'VideoKeyword',
>     order: :start,
>     eager: :keyword
>
>
>
> related_videos returns all the video that share a common keyword
>
> I'd like to make it work with eager loading, but I'm not sure what is the 
> right approach.
>

Start with reading 
http://sequel.jeremyevans.net/rdoc/files/doc/advanced_associations_rdoc.html#label-Two+basic+example+eager+loaders

The eager loader will have to get all keyword ids from all current videos, 
then find videos with any of those keywords, then associate each of those 
videos with the current videos that have those keywords, probably using a 
hash with keyword_id keys and arrays of current videos.

For the example you gave, I think you can just use the many_through_many 
plugin:

  plugin :many_through_many
  many_through_many :related_videos, [[:video_keywords, :video_id, 
:keyword_id],   [:keywords, :id, :id], [:video_keywords, :keyword_id, 
:video_id]]

That's simpler and should provide you with a working eager loader.

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