Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 362797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:16:53+00:00 2026-05-12T13:16:53+00:00

I am trying to accomplish the atypical library learning a language application. I have

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:16:53+00:00Added an answer on May 12, 2026 at 1:16 pm

    Perhaps:

    class Book < ActiveRecord::Base
      has_many :book_loans
      has_many :borrowers, :class_name => 'Person', :through => :book_loans
    
      def loaned?
        book_loans.exists?(:return_date => nil)
      end
    
      # I would be reluctant to return a new Person object
      #  just because it was not checked out by anyone, instead you could return nil
      #  OR exception out.   
      def current_borrower
        book_loans.first(:order => "out_date desc").person 
      end
    end
    
    # you can use a helper to keep your presentation clean 
    module BookHelper 
      def borrower_name(book)
         if borrower = book.borrower
           borrower.name 
         else 
           "not checked out" 
         end
      end
    end 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to accomplish something that seemed quite simple... I have 3 div
I have been trying to get this code to work for a while now
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
What I'm trying to accomplish My app generates some tabular data I want the
So I am trying to accomplish something like this: SELECT * FROM table WHERE
I could probably write this myself, but the specific way I'm trying to accomplish
I am trying to determine the best time efficient algorithm to accomplish the task
I am trying to create a rewrite rule that accomplishes two things: Redirect (www.)?domain.com
Hokay so here is what I'm trying to accomplish: I'm going to be sending
Im trying to accomplish something like this. I want to add the text stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.