OK, I fell like we're getting towards the end here (I'm guessing you're
hoping we are).
In the purchase method:
<<
def purchase(book, copies=1)
bl = book_location(book)
bl ?
bl.update(:copies=>bl.total+copies) :
BookLocation.create(:book=>book, :location=>self,
:total=>copies, :checked_out=>0)
end
>>
the bl = book_location(book) line says that book_location is undefined, but
if I use
bl = book_locations(book), it works, but as you noted, I do get back an
array. Should the book_location() work?
Also, I'm a little unclear of the singular / plural. In my tables, I create
the book/location join table as book_locations. It looks like that then
flowed through the models. Should I have made that books_locations with them
both plural and then used that in the models?
Once again, thanks for your patience.
Scott
On Tue, Jul 14, 2009 at 9:21 AM, Scott LaBounty <[email protected]> wrote:
> Jeremy,
>
> Thanks, it made it through my test at least. I'll give the real code a try
> next. I can't tell you how much I appreciate the help I receive on this
> list.
>
> Scott
>
>
> On Tue, Jul 14, 2009 at 9:00 AM, Jeremy Evans <[email protected]>wrote:
>
>>
>> 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
>> >>
>>
>
>
> --
> 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
-~----------~----~----~----~------~----~------~--~---