On Wed, Sep 30, 2020 at 9:56 AM Rodrigo Dutra <[email protected]> wrote:

> Not a failure per say, just wondering what's the proper way of doing this
> we have a custom adder in one of our models like so
>
> many_to_many :disabled_custom_products,
>                              class: 'CustomProduct',
>                              join_table:
> :disabled_custom_products_locations,
>                              right_key: :custom_product_id, left_key:
> :location_id,
>                              adder: (lambda do |custom_product, reason,
> comment, available_at = nil|
>
>  DisabledCustomProductsLocation.update_or_create(
>                                              { location_id: id,
> custom_product_id: custom_product.id },
>                                               available_at: available_at,
> reason: reason, comment: comment)
>                                            end)
>
> My problem is that that custom adder has gotten too big and I'd like to
> move it off the association declaration, but I've found that I can only
> declare the lambda as a variable and have to do do it before the
> association declaration (I can probably create a method too but that would
> look even uglier defined before the relations)
>
> Is there a better way of accomplishing this ?
>

If the issue is only about the lambda being too big, you could do:

adder: lambda{|*args| your_method(*args)}

def your_method(custom_product, reason, comment, available_at = nil)
  DisabledCustomProductsLocation.update_or_create(
    { location_id: id, custom_product_id: custom_product.id },
     available_at: available_at, reason: reason, comment: comment)
end

An alternative approach is to do this after the association and not use the
:adder option, since the :adder option just defines this method:

private def _add_disabled_custom_product(custom_product, reason, comment,
available_at = nil)
  DisabledCustomProductsLocation.update_or_create(
    { location_id: id, custom_product_id: custom_product.id },
     available_at: available_at, reason: reason, comment: comment)
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSSfcxzOH3LQYLFZumMmtEvanmw0%2BGt0vgjywr-cOGyjyFg%40mail.gmail.com.

Reply via email to