Custom dataset methods force duplicate code.

Example of desired code:

Sequel::Model.plugin :dataset_associations

class Area < Sequel::Model
  # has PostgreSQL `polygon`
  
  many_to_one :album

  one_to_many :included_locations, class: 'Location' do |ds|
    ds
      .join(
        Sequel[:areas].as(:joined_areas),
        Sequel.lit('locations.point <@ joined_areas.polygon')
      )
      .where(
        Sequel[:joined_areas][:id] =>
          defined?(id) ? id : Sequel[:areas][:id]
      )
  end
end

class Album < Sequel::Model
  # has `selling_areas` with PostgreSQL `polygon`

  one_to_many :selling_areas, class: Area

  one_to_many :potential_listeners, class: 'Listener' do |ds|
    selling_areas.included_locations.listeners
  end
end

class Location < Sequel::Model
  # has PostgreSQL `point`
  one_to_many :listeners
  
  one_to_many :reaching_areas, class: Area do |ds|
    ds
      .join(
        Sequel[:locations].as(:joined_locations),
        Sequel[:joined_locations][:id] =>
          defined?(id) ? id : Sequel[:locations][:id]
      )
      .where(Sequel.lit('"polygon" @> locations.point') )
  end
end

class Listener < Sequel::Model
  # has `locaiton` with PostgreSQL `point`

  many_to_one :locaiton

  one_to_many :available_albums, class: Album do |ds|
    location.reaching_areas.albums
  end
end

In some cases I need for `available_albums` for `Listener` instance, in 
other for dataset (for counts in list), and I need for 
`potential_listeners` of `Album` instance too :(


On Tuesday, December 19, 2017 at 6:52:14 PM UTC+3, Jeremy Evans wrote:
>
> On Tuesday, December 19, 2017 at 7:05:44 AM UTC-8, Alexander Popov wrote:
>>
>> But `:dataset` option doesn't work with 
>> `Sequel::Plugins::DatasetAssociations`… Okay, maybe I should use regular 
>> dataset methods instead of associations.
>>
>
> I don't think dataset_associations can support custom associations.  You'd 
> have to add your own dataset method manually for custom dataset 
> associations.
>
> 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