Continuing access to the many_to_many join table when adding an association

I am creating a wish list system using Sequel. I have a wishlists and items table and a items_wishlists connection items_wishlists (this name is a sequel). The items_wishlists table also has an extra column for the facebook id (so I can store opengraph actions), which is a NOT NULL column.

I also have Wishlist and Item models in which the many_to_many association is set. The Wishlist class also has a :select parameter for the many_to_many association set to select: [:items.*, :items_wishlists__facebook_action_id] .

Is there a way to add additional data when creating an association, for example wishlist.add_item my_item, facebook_action_id: 'xxxx' or something else? I cannot do this after creating the association, since the facebook id has NOT NULL in the column.

Thanks for any help

+4
source share
2 answers

The recommended way to do this is to add a model to the connection table. However, if you do not want to do this, you can do:

 class Wishlist def _add_item(item, hash={}) model.db[:items_wishlists].insert(hash.merge(:item_id=>item.id, :wishlist_id=>id)) end end 
+7
source

I think there is another possibility.

First MWE (minimum working example) of your question:

  require 'sequel' Sequel.extension :pretty_table #Sequel::PrettyTable.print()/Sequel::PrettyTable.string() DB = Sequel.sqlite DB.create_table(:wishlists){ primary_key :id String :listname } DB.create_table(:items){ primary_key :id String :descr } DB.create_table(:items_wishlists){ primary_key :id foreign_key :wishlist_id, :wishlists foreign_key :item_id, :items add_column :facebook_id, type: :nvarchar } class Item < Sequel::Model many_to_many :wishlists end class Wishlist < Sequel::Model many_to_many :items end w1 = Wishlist.create(listname: 'list1') w1.add_item(Item.create(descr: 'item 1')) #w1.add_item(Item.create(descr: 'item 2'), facebook_id: 'fb2') ##<- This does not work #Sequel::PrettyTable.print(Wishlist) #Sequel::PrettyTable.print(Item) Sequel::PrettyTable.print(DB[:items_wishlists]) 

To enable ad_item with the parameter ( Wishlist#add_item(Item.create(descr: 'item 2'), facebook_id: 'fb2') ), you must define adder , as in this example:

 require 'sequel' Sequel.extension :pretty_table Sequel::PrettyTable.print()/Sequel::PrettyTable.string() DB = Sequel.sqlite DB.create_table(:wishlists){ primary_key :id String :listname } DB.create_table(:items){ primary_key :id String :descr } DB.create_table(:items_wishlists){ primary_key :id foreign_key :wishlist_id, :wishlists foreign_key :item_id, :items add_column :facebook_id, type: :nvarchar } class Item < Sequel::Model #~ many_to_many :wishlists end class Wishlist < Sequel::Model many_to_many :items, join_table: :items_wishlists, class: Item, left_key: :wishlist_id, right_key: :item_id, adder: (lambda do |item, facebook_id: nil| self.db[:items_wishlists].insert(wishlist_id: self.id, item_id: item.id, facebook_id: facebook_id) end) end w1 = Wishlist.create(listname: 'list1') w1.add_item(Item.create(descr: 'item 1')) w1.add_item(Item.create(descr: 'item 2'), facebook_id: 'fb2') Sequel::PrettyTable.print(DB[:items_wishlists]) 

Result:

 +-----------+--+-------+-----------+ |facebook_id|id|item_id|wishlist_id| +-----------+--+-------+-----------+ | | 1| 1| 1| |fb2 | 2| 2| 1| +-----------+--+-------+-----------+ 

But the following problem will come later:

With w1.items you get a list of items, but you do not have access to the parameters. (at least I have not found a way so far. I am still studying, but I expect that I need a join table model for this (see Jeremis recommendation))

+1
source

All Articles