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

        create_table(:books_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)
        bl = book_locations(book)
        bl ?
            bl.update(:copies=>bl.total+copies) :
            BookLocation.create(:book=>book, :location=>self,
:total=>copies, :checked_out=>0)
    end
end

class BookLocation < Sequel::Model(:books_locations)
    many_to_one :books
    many_to_one :locations
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.

Thanks again for the help.

Scott

On Sun, Jul 12, 2009 at 10:17 AM, Scott LaBounty <[email protected]>wrote:

> Jeremy,
>
> Thanks I'll mull that over and see what I can do with it. I appreciate all
> the help.
>
> Scott
>
>
> On Sat, Jul 11, 2009 at 10:39 PM, Jeremy Evans <[email protected]>wrote:
>
>>
>> On Jul 11, 7:45 pm, Scott LaBounty <[email protected]> wrote:
>> > OK, I'm getting confused with the foo, bar, baz thing. I'm trying to do
>> > something very similar (in fact I was going to ask before I saw this
>> > thread). I have a library app. with books and locations. Each book can
>> have
>> > many locations and each location can have many books. What I'm trying to
>> add
>> > is a "total" and a "checked_out" value for each book at each location.
>> Based
>> > on the above, what I have for my tables are (from my migration):
>> >
>> >
>> >         create_table(:books) do
>> >             primary_key :id
>> >             String :title
>> >         end
>> >
>> >         create_table(:locations) do
>> >             primary_key :id
>> >             String :location
>> >         end
>> >
>> >         create_table(:books_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
>> > end
>> >
>>  class BookLocation < Sequel::Model
>>     many_to_one :book
>>     many_to_one :location
>> >  end
>> >
>> > The BookLocations is what I gather from Jeremy's post that I need
>> (should
>> > those lines be "many_to_many" though?
>> >
>> > Now though, I'm not sure what I need to add to the Book model to let me
>> add
>> > count and checked_out values (and change them) for a given destination.
>> >
>> > Thoughts?
>>
>> The association modification methods are fairly generic, for domain
>> specific behavior like you probably need, you probably should write
>> your own methods that use the BookLocation model.  Something like:
>>
>>  class Location
>>    def purchase(book, copies=1)
>>      bl = book_location(book)
>>      bl ? bl.update(:copies=>bl.copies+copies) : BookLocation.create
>> (:book=>book, :location=>self, :total=>copies, :checked_out=>0)
>>    end
>>    def sold(book, copies=1)
>>      bl = book_location(book)
>>      bl.update(:copies=>bl.copies-copies)
>>    end
>>    def checkout(book)
>>      bl = book_location(book)
>>      bl.update(:checked_out=>bl.copies+1)
>>    end
>>    def checkin(book)
>>      bl = book_location(book)
>>      bl.update(:checked_out=>bl.copies-1)
>>    end
>>    def book_location(book)
>>      book_locations_dataset[:book_id=>book.id]
>>    end
>>  end
>>
>> That's just a guess based on the probable needs of your app.
>>
>> Jeremy
>> >>
>>
>
>
> --
> 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