# Accessing @org.id yields internal number, not record id

**URL:** <https://www.ruby-forum.com/t/accessing-org-id-yields-internal-number-not-record-id/100629>\
**Category:** Rails\
**Created:** [July 31, 2006, 1:24am UTC](https://www.ruby-forum.com/t/accessing-org-id-yields-internal-number-not-record-id/100629 "2006-07-31T01:24:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 31, 2006, 1:24am UTC](https://www.ruby-forum.com/t/accessing-org-id-yields-internal-number-not-record-id/100629/1 "2006-07-31T01:24:58Z")

</div>

With apologies, I can’t figure out the simplest thing: How to reference  
a record id instead of the internal memory location of that value. I’m  
new to Ruby but otherwise a veteran VB6/SQL programmer.

My code:

```
 sSQL = "SELECT id, org_name FROM organizations WHERE user_id = 

```

‘#{sUserId}’ AND user\_password = ‘#{sPassword}’"

```
 @org = Organization.find_by_sql sSQL

 if @org

    write_log("Org found. Id = #{@org.id}")

```

My log file contains:

Org found. Id = 30382648

I am expecting Id to be 1.  
There are only two records in the organizations table: 1 and 2.

Thanks for putting me straight!

Michael

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 31, 2006, 1:46am UTC](https://www.ruby-forum.com/t/accessing-org-id-yields-internal-number-not-record-id/100629/2 "2006-07-31T01:46:48Z")

</div>

Michael Wilkes wrote:

> ```
> @org = Organization.find_by_sql sSQL
> 
> if @org
> 
> write_log("Org found. Id = #{@org.id}")
> 
> ```

find\_by\_sql returns an Array (since you can select multiple records).

You should also consider using parameters to avoid the potential for SQL  
injection with the above code:

@org = Organization.find\_by\_sql([“SELECT id, org\_name FROM organizations  
WHERE user\_id = ? AND user\_password = ?”, sUserId, sPassword]).first

–  
Philip R.  
[http://tzinfo.rubyforge.org/](http://tzinfo.rubyforge.org/) – DST-aware timezone library for Ruby

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 31, 2006, 5:28am UTC](https://www.ruby-forum.com/t/accessing-org-id-yields-internal-number-not-record-id/100629/3 "2006-07-31T05:28:07Z")

</div>

# org = Organization.find :first, :conditions = ["user\_id = ? and password

?", sUserId, sPassword]

logger.info “Org found. Id = #{@org.id}”

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [August 1, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/accessing-org-id-yields-internal-number-not-record-id/100629/4 "2006-08-01T18:28:32Z")

</div>

> logger.info “Org found. Id = #{@org.id}”

Thanks for the help to both responders. There’s something I still don’t  
understand tho because attempts to access @org as an array fail for me.

==== code ======

```
 sSQL = "SELECT id, org_name FROM organizations WHERE user_id = 

```

‘#{sUserId}’ AND user\_password = ‘#{sPassword}’"

write\_log(sSQL)

```
 @org = Organization.find_by_sql(sSQL).first

```

if @org.nil?  
write\_log(“Org is nil”)  
else  
write\_log("Org list returned: " + @org.inspect)  
end

================

Results in log:

SELECT id, org\_name FROM organizations WHERE user\_id = ‘kevin’ AND  
user\_password = ‘password’

Org list returned: #\<Organization:0x38130e0 @attributes={“id”=\>“1”,  
“org\_name”=\>“Carlie Couches”}\>

===============

When I try to access @org.id, however, I get errors.  
For instance, this line:  
write\_log(“Org list returned: #{@org.id}”)  
throws this error:  
Called id for nil, which would mistakenly be 4 – if you really  
wanted the id of nil, use object\_id

This line:  
write\_log(“Org list returned: #{@org.org\_name}”)  
throws the same error.

This line:  
write\_log(“Org list returned: #{@org[0].org\_name}”)  
throws this error:  
You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.org\_name

I’ve looked at it with inspect and to\_xml and there is always data  
there.  
How do I get to it? I’m missing something basic here.

Thanks for helping,  
Michael

P.S. I couldn’t figure out how to pass quoted strings as parameters,  
that’s why i’m using the sSQL statement instead of parameters. Any  
example of passing a quoted string using the ? parameter method would be  
helpful too. Passing the strings without accounting for the quotes gave  
SQL syntax errors because of empty strings coming through.
