On Fri, Nov 19, 2021 at 2:00 PM adam.me...@gmail.com < adam.mediadr...@gmail.com> wrote:
> Hi Jeremy, > > I'm working on something that has me a bit puzzled. I'd like to introduce > an association that depends on a provided value, or fall back to a default. > > My plan is to overwrite the getter to accept an optional argument, and > then to pass it along. But using `super` won't work. I have a method that > does, but it's the first time I've had to alias a method inside a Sequel > model and I wonder if there's a better way. > > Here's the code and annotated spot. > > DB.create_table(:subscriptions) do > primary_key :id > end > > DB.create_table(:discounts) do > primary_key :id > column :valid_until, Date > foreign_key :subscription_id, :subscriptions > end > > class Subscription < Sequel::Model > one_to_many :discounts > > one_to_one :active_discount, class: :Discount do |ds| > ds.where { valid_until >= Sequel.delay { |ds| ds.opts[:invoice_date] || > Sequel::CURRENT_TIMESTAMP }} > end > > # Is there a better way than using an alias here? super won't work inside > the method > alias_method :original_active_discount, :active_discount > > def active_discount(invoice_date = nil) > if invoice_date > original_active_discount { |ds| ds.clone(invoice_date: invoice_date) } > else > original_active_discount > end > end > You can use the alias approach, but you don't need to, because association methods are added to a module included in the class, not in the class itself. You can use super, but you need the right syntax: def active_discount(invoice_date = nil) if invoice_date super() { |ds| ds.clone(invoice_date: invoice_date) } else super() end end 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 sequel-talk+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/CADGZSSesL-echA_i7VAk6JAJWhwa%2B5AED%3D44c0VpD7yPXKOrVA%40mail.gmail.com.