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

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

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/ – DST-aware timezone library for Ruby

org = Organization.find :first, :conditions = ["user_id = ? and password

?", sUserId, sPassword]

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs