On Monday, June 1, 2020 at 9:43:12 AM UTC-7, Aryk Grosz wrote:
>
> Is there a way change the dataset module as you are joining multiple
> associations and tables?
>
> For example:
>
> class User < Sequel::Model
>
> dataset_module do
> def with_min_age(min_age, u = Sequel[model.table_name])
> condition = Sequel.lit("? <= current_date - (? || '
> years')::interval", u[:birth_date], min_age)
> condition = Sequel.|({min_age => nil}, condition) if
> min_age.is_a?(Sequel::SQL::Expression)
> where(condition)
> end
> end
> end
>
>
> So I can use this as users_dataset.with_min_age(4).
>
> But I'd like to be able to also do:
>
> books_dataset.association_join(:user).with_min_age(Sequel[:books][:min_age_required],
>
> Sequel[:user])
>
> To be able to reuse the methods.
>
> Is something like this possible?
>
Sure. You can pass a module to dataset_module
module Foo
def with_min_age(min_age, u = Sequel[model.table_name])
condition = Sequel.lit("? <= current_date - (? || '
years')::interval", u[:birth_date], min_age)
condition = Sequel.|({min_age => nil}, condition) if
min_age.is_a?(Sequel::SQL::Expression)
where(condition)
end
end
User.dataset_module Foo
Book.dataset_module Foo
Of course, with books, the method may not make sense until you join to the
users table.
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 view this discussion on the web visit
https://groups.google.com/d/msgid/sequel-talk/6398f8b9-7ea2-485e-9f42-cfea07cdb6a6%40googlegroups.com.