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

class Book < Sequel::Model
end
class Tune < Sequel::Model
end
class Tuning < Sequel::Model
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_tune_tuning(tune1, tuning1)
book2.add_tune_tuning(tune2, tuning1)

# etc.
>>

I know in the say book model I'd need something like ...

    many_to_many :???, :class => Tuning,
        :join_table => :books_tunes_tunings, :left_key => :book_id,
:right_key => :tuning_id

but I'm not sure of what makes sense in the "???" part of if I have the
left/right key portion correct.

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?

Thanks again,

Scott


On Sat, Dec 11, 2010 at 8:59 PM, Scott LaBounty <[email protected]> wrote:

> Thanks for the quick help Jeremy. I'll give it a try.
>
> Scott
>
>
> On Sat, Dec 11, 2010 at 8:24 PM, Jeremy Evans <[email protected]>wrote:
>
>> On Dec 11, 7:46 pm, Scott LaBounty <[email protected]> wrote:
>> > All,
>> >
>> > I have three items, books, tunes, and keys. A book can have multiple
>> tunes,
>> > and a tune can appear in multiple books in different keys. I'm not quite
>> > sure how to express this either as database tables or models. If it were
>> > just say books and tunes, I'd have a join table between them and
>> > many_to_many relationships. I'm just not sure how to add in the third
>> piece.
>>
>> You want a three way join table:
>>
>> books_tunes_keys:
>>  book_id references books
>>  tune_id references tunes
>>  key_id references keys
>>
>> You can use a standard Sequel many_to_many relationship, but you will
>> need to specify the :join_table=>:books_tunes_keys option.
>>
>> 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]<sequel-talk%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/sequel-talk?hl=en.
>>
>>
>
>
> --
> Scott
> http://steamcode.blogspot.com/
>



-- 
Scott
http://steamcode.blogspot.com/

-- 
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