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 583815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:49:57+00:00 2026-05-13T14:49:57+00:00

Let me clarify my question. I’m trying to get an integer value from @student.rooms.last,

  • 0

Let me clarify my question. I’m trying to get an integer value from @student.rooms.last, and it doesn’t seem that @student.rooms.last alone will give me an integer value.

Student.transaction do
if @student.test!
 x = @student.site_id.to_int
 y = @student.rooms.last

book = Book.find(:first, :conditions => ["location_id = ? AND room_id = ?", x, y])

   room = Room.new
   room.student_id = @student.id 
   if room.save
     book.room_id = room.id

Right now this returns an error: You have a nil object when you didn’t expect it! The error occurred while evaluating nil.room_id=
I’m trying to find the room record associated with @student that has an id equal to the book room_id foreign key. Thanks for any response.

  • 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-13T14:49:57+00:00Added an answer on May 13, 2026 at 2:49 pm

    I imagine is the issue that z only exists in the block scope, and needs to be defined outside.

    rooms = @student.rooms
    z = nil
    rooms.each do |room|
      if room.id == @student.rooms.length - 1
      z = room.id
    end
    end
    book = Book.find(:first, :conditions => ["room_id = ?", z])
    

    Really, though, why aren’t you doing:

    book = @student.rooms.last.books.first
    

    ? From my vague understanding of your script, it seems like it’s the same thing.

    And it almost feels like your code would break if the books belonged to students other than your first one, since @student.rooms.length - 1 would only be the ID of that student’s last book if, say, the student owned 5 books with IDs 1-4… no, wait. That code should return the second-to-last book, right? And only if the student owns the very first books in the database?

    Bah, whatever. Just use classic ActiveRecord methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Caveats: Let me first clarify that this is not a question about whether to
Let me clarify my question, and the solution I'm looking for. I'm using wikispaces.com,
Let me try to clarify my question, I wasn't sure how to word the
Before anything, let me first clarify that the below thoughts are purely my personal
To clarify my question, let's start off with an example program: #include <stdio.h> #pragma
Realize this may sound like a broad question - so let me clarify. I
Let me try to explain what I need. I have a server that is
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let me preface this question by saying I use TextMate on Mac OSX for

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.