`:dataset` option is unreachable in another dataset method of model. You 
can write `album.potential_listeners_dataset`, but you can't 
`Album.potential_listeners_dataset`. But OK, I understood you, thanks.

On Tuesday, December 19, 2017 at 7:59:04 PM UTC+3, Jeremy Evans wrote:
>
> On Tuesday, December 19, 2017 at 8:01:00 AM UTC-8, Alexander Popov wrote:
>>
>> Custom dataset methods force duplicate code.
>>
>
> Sometimes duplicate code is necessary.
>  
>
>> 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
>>
>
> This is probably going to break things, the block should return a modified 
> dataset, not a complete new dataset.
>
> You probably want to join or use a subquery in the block, and you may want 
> to use the :dataset option instead of a block.
>
> Associations can't handle every possible case.  In more complex cases, you 
> will need to write the dataset code manually.
>
> 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