I am trying to accomplish the atypical library learning a language application. I have been able to create Books, People, and BookCheckOuts. I've got the relationships working pretty well but am having issues with wrapping my head around how to handle books that have never been checked out.
I've created two properties on my book class CheckedOut (returns a boolean) and LastCheckedOutTo (returns a person). I am pretty much at peace with CheckedOut and feel confident that I am using the right RoR mechanism for determining if a book is presently checked out and returning a boolean in either case. I am not as confident about LastCheckedOutTo as my implementation seems like a kludge.
Am I going about this correctly? Is there a better way?
Book Class in its Entirety
class Book < ActiveRecord::Base
has_many :book_check_outs
has_many :people, :through => :book_check_outs
def checked_out
if (book_check_outs.find(:first, :conditions => "return_date is null"))
true
else
false
end
end
def last_checked_out_to
if (book_check_outs.count > 0)
book_check_outs.find(:first,
:order => "out_date desc").person
else
Person.new()
end
end
end