On Mar 9, 9:09 am, Scott LaBounty <[email protected]> wrote:
> Once again ignorance of both databases in general and Sequel specifically.
> If I have the following code:
>
> ------------------------------------
> require 'rubygems'
> require 'sequel'
>
> DB = Sequel.sqlite # Create an in-memory database
>
> DB.create_table :books do
>     primary_key :id
>     column :title, :text, :unique=>true
>     column :population, :integer
> end
>
> DB.create_table :authors do
>     primary_key :id
>     column :name, :name
> end
>
> DB.create_table :authors_books do
>     primary_key :id
>     foreign_key :book_id
>     foreign_key :author_id
> end
>
> class Book < Sequel::Model
>     many_to_many :author
> end
>
> class Author < Sequel::Model
>     many_to_many :book
> end
>
> # Is this needed?
> class Author_Book < Sequel::Model; end

No, that's not needed, though it doesn't harm anything.

> cProgrammingLanguage = Book.create(:title => 'The C Programming Language')
> kernighan = Author.create(:name => 'Brian Kernighan')
> ritchie = Author.create(:name => 'Dennis Ritchie')
> cProgrammingLanguage.add_author(kernighan)
> cProgrammingLanguage.add_author(ritchie)
> kernighan.add_book(cProgrammingLanguage)
> ritchie.add_book(cProgrammingLanguage)

You don't need to add it both ways.  If you do

  cProgrammingLanguage.add_author(kernighan)

then:

  kernighan.books.include(cProgrammingLanguage).should == true

> # cProgrammingLanguageAuthors = cProgrammingLanguage.authors_dataset
> # dennisRitchieBooks = ritchie.books_dataset
>
> ------------------------------------
>
> Do I need the Author_Book model? Did I name if right if I did? How do I get
> all of the authors for a book? How do I get all of the books an author
> wrote.

book.authors is an array of Author instances
author.books is an array of Book instances
book.authors_dataset is the dataset representing the author's books
author.books_dataset is the dataset representing the book's authors

> Once again, I've really appreciated all the help I've received on this list.
> I'll probably be documenting what I find (OK, what you tell me) on my blog,
> so that hopefully others will be able to find this information as they're
> just starting out.

You're welcome.  Blog posts explaining Sequel's features are much
appreciated.

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