On Friday, July 5, 2019 at 7:41:02 AM UTC-7, Artem Kondratev wrote:
>
> Let's say we have a class
>
> class BankAccount < Sequel::Model do
>   plugin :subclasses
>
> end
>
> And it's subclass
>
> class BankAccountSub < BankAccount
> end
>
> If we are going to call it's methods like
>
> BankAccountSub.all
>
> We are going to get all BankAccounts.
> To get only BankAccountSubs (we are assuming we can BankAccountSubs should 
> not have `attr` attribute (=nil)
> One possible solution is to override method like this (in this case we 
> will get all 
>
> class BankAccountSub < BankAccount
>   def self.all
>     exclude(attr: nil)
>   end
> end
>
> This will work fine, but overriding all of the methods is too much work 
> and it is always possible to miss something.
>
> So the question is - is there any way to create Subclassed the way that 
> all class methods take into account this `attr`'s value?
>

You probably want something like:

class BankAccountSub < BankAccount
  set_dataset dataset.where(attr: nil)
end

This is not a perfect solution, though.  It has various corner cases:

# Includes rows where bar = 2 and attr IS NOT NULL
BankAccountSub.where(:foo=>1).or(:bar=>2)

# Creates a record that would not be retrievable later through 
BankAccountSub
BankAccountSub.create(:attr=>'a')

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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/75ea6621-33cc-4932-b2ca-d3bd2a3f48f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to