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