managing join table additional attributes

13 views
Skip to first unread message

John Knapp

unread,
Sep 4, 2021, 8:41:11 PMSep 4
to sequel-talk
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

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)

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)

I feel I must be missing something obvious but I've studied the docs and posts on here and have struck out! 

I'd be happy to author documentation for this once I learn.

--JK

Rob Galanakis

unread,
Sep 5, 2021, 3:23:14 PMSep 5
to sequel-talk
To me, that seems like you'd want a separate model, not a join table. - Rob

John Knapp

unread,
Sep 5, 2021, 5:33:41 PMSep 5
to sequel-talk
Thanks Rob, I actually have both but we can forget that model for the moment. The join table has an additional column "quantity". My objective is learning the correct sequel syntax to add a specified quantity of a given capability (c) into a plan (p) while using the sequel add_association method. (See my two pseudo code examples of syntax that does not work.) What syntax does?

Rob Galanakis

unread,
Sep 6, 2021, 11:39:38 AMSep 6
to sequel-talk
Jeremy can answer authoritatively but AFAIK it does not exist and is a bit of an oxymoron- "join tables" as Sequel defines them are just a compound key; adding an additional attribute ceases to make them a join table.
I'm not going to explain why you should use a separate model for this since you didn't ask for it, but having been down this road I would definitely say use a separate model.
- Rob

Jeremy Evans

unread,
Sep 7, 2021, 3:15:34 PMSep 7
to seque...@googlegroups.com
On Sat, Sep 4, 2021 at 5:41 PM John Knapp <j...@skillbank.io> 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 
Reply all
Reply to author
Forward
0 new messages