On Dec 12, 11:18 am, Scott LaBounty <[email protected]> wrote:
> OK, I got the table creation piece, but I'm still not sure how to set up the
> models. Here's what I have and what I'd like to work (changed key to tuning
> because that was getting too confusing for an example) ...
>
> <<
> require 'rubygems'
> require 'sequel'
>
> DB = Sequel.sqlite
>
> DB.create_table :books do
>     primary_key :id
>     column :title, :text, :unique=>true
> end
>
> DB.create_table :tunes do
>     primary_key :id
>     column :title, :text, :unique=>true
> end
>
> DB.create_table :tunings do
>     primary_key :id
>     column :tuning, :text, :unique=>true
> end
>
> DB.create_table :books_tunes_tunings do
>     primary_key :id
>     foreign_key :book_id, :books
>     foreign_key :tuning_id, :tunings
>     foreign_key :tune_id, :tunes
> end


For a three way join table, you usually want to use a join model:

> class Book < Sequel::Model
  one_to_many :book_tune_tunings
  many_to_many :tunes, :join_table=>:books_tunes_tunings
  many_to_many :tunings, :join_table=>:books_tunes_tunings
> end
> class Tune < Sequel::Model
  one_to_many :book_tune_tunings
  many_to_many :books, :join_table=>:books_tunes_tunings
  many_to_many :tunings, :join_table=>:books_tunes_tunings
> end
> class Tuning < Sequel::Model
  one_to_many :book_tune_tunings
  many_to_many :books, :join_table=>:books_tunes_tunings
  many_to_many :tunes, :join_table=>:books_tunes_tunings
> end

class BookTuneTuning < Sequel::Model(:books_tunes_tunings)
  many_to_one :book
  many_to_one :tune
  many_to_one :tuning
end

> book1 = Book.create(:title => 'Book 1')
> book2 = Book.create(:title => 'Book 2')
>
> tune1 = Tune.create(:title => 'Tune 1')
> tune2 = Tune.create(:title => 'Tune 2')
>
> tuning1 = Tuning.create(:tuning => 'C')
> tuning2 = Tuning.create(:tuning => 'D')

book1.add_book_tune_tuning(:tune=>tune1, :tuning=>tuning2)
book2.add_book_tune_tuning(:tune=>tune2, :tuning=>tuning1)

> And ... ask long as I'm bothering everyone, how do I actually put
> information in the join table? I'd also like to have a page number there (at
> least that's where it makes sense to me). Is that the right way to do it?
> How would I in the "add_tune_tuning" method also specify the page number?

You'd just add a page_number column to the books_tunes_tunings table.
With a join table model, it's really easy to handle, you'd just add a
hash entry for :page_number when calling the add_book_tune_tuning.

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to