On Sat, Sep 4, 2021 at 5:41 PM John Knapp <[email protected]> wrote:
> I have a standard many_to_ many with a model for the join table. The join
> table has an extra attribute, "quantity".
>
> class Plan < Sequel::Model
> many_to_many :capabilities
> end
>
> # join table with extra column "quantity"
> class CapabilitiesPlans < Sequel::Model
> end
>
Note that you would generally want to name with CapabilityPlan, as model
class names should use the singular form. You'll have to specify the table
name to do that.
> class Capability < Sequel::Model
> many_to_many :plans
> end
>
> I can create the join record directly but that doesn't feel like the
> sequel way.
> CapabilitiesPlans.create(capability_id: c.id, plan_id: p.id,
> quantity:5)
>
This is the recommended way.
> But how can I modify this syntax to add quantity?
> p.add_capability(c)
> How to add the extra attribute using the add_*association* method?
>
> These two don't work:
> p.add_capability(c).quantity(5)
> p.add_capability(c, quantity: 5)
>
The second one will work, but you need a custom :adder option in the
capabilities association. Something like:
adder: lambda {|cap, att={}|
CapabilitiesPlans.create(capability_id: cap.id, plan_id: id, **att)
}
I feel I must be missing something obvious but I've studied the docs and
> posts on here and have struck out!
>
How to modify how the add_* method works is mentioned in the documentation,
but there is definitely a lot of documentation and this particular need
isn't highlighted:
http://sequel.jeremyevans.net/rdoc/files/doc/association_basics_rdoc.html#label-3Aadder+-28_add_association+method-29
> I'd be happy to author documentation for this once I learn.
>
I wouldn't be opposed to an additional example in that section if you want
to send in a PR.
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/CADGZSSeba%3DSCjDZijQxdOZEp%3DdzxGMAgd6dFYZGqqZ-9rhCqYQ%40mail.gmail.com.