On Jul 14, 8:52 am, Scott LaBounty <[email protected]> wrote:
> OK, I'm still having some issues with this. Here's what I ended up with in
> my migrations:
>
> <<
>         create_table(:books) do
>             primary_key :id
>             String :title
>         end
>
>         create_table(:locations) do
>             primary_key :id
>             String :location
>         end

First, make sure you use the same join table name:

        create_table(:book_locations) do
>             primary_key :id
>             foreign_key :location_id, :locations
>             foreign_key :book_id, :books
>             Integer :total
>             Integer :checked_out
>         end
>
>
>
> and my models:
>
> <<
> class Book < Sequel::Model
>     one_to_many :book_locations
>     many_to_many :locations, :join_table=>:book_locations
> end
>
> class Location < Sequel::Model
>     one_to_many :book_locations
>     many_to_many :books, :join_table=>:book_locations
>
>     # Create a class method to grab the id, and locations from the default
>     # Location dataset.
>     def self.all_locations
>         select(:id, :location).all
>     end
>
>     def purchase(book, copies=1)

I'm not sure this will work as you think.  book_locations will return
an array of BookLocations, not a single one for the book.  You
probably want the book_location method I used earlier.

>         bl = book_locations(book)
>         bl ?
>             bl.update(:copies=>bl.total+copies) :
>             BookLocation.create(:book=>book, :location=>self,
> :total=>copies, :checked_out=>0)
>     end
> end

many_to_one takes a singular, not a plural:

  class BookLocation < Sequel::Model(:book_locations)
      many_to_one :book
      many_to_one :location
> end
>
>
>
> and here's some test code I wrote:
>
> b = Book.create(:title => 'Snow Crash')
> l = Location.create(:location => 'Irvine')
> bl = BookLocation.create(:book=>b, :location=>l, :total=>1, :checked_out =>
> 0)
>
> When I try to add the book location (similar to the purchase() method
> above), I get an error:
>
> Sequel::Error: method book= doesn't exist or access is restricted to it.

Fixing the many_to_one to be singular should solve this.

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