Michael Wilkes
2006-Jul-30 23:25 UTC
[Rails] 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: <snip> 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}") </snip> 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 -- Posted via http://www.ruby-forum.com/.
Philip Ross
2006-Jul-30 23:44 UTC
[Rails] Re: Accessing @org.id yields internal number, not record id
Michael Wilkes wrote:> 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: > > <snip> > > 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}") > > > </snip>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 Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby
Hammed Malik
2006-Jul-31 03:25 UTC
[Rails] Accessing @org.id yields internal number, not record id
org = Organization.find :first, :conditions = ["user_id = ? and password ?", sUserId, sPassword] logger.info "Org found. Id = #{@org.id}" On 7/31/06, Michael Wilkes <vbsql7@gmail.com> wrote:> > 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: > > <snip> > > 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}") > > > </snip> > > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060731/11d0640a/attachment.html
Michael Wilkes
2006-Aug-01 16:28 UTC
[Rails] Re: Accessing @org.id yields internal number, not record 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. -- Posted via http://www.ruby-forum.com/.
Maybe Matching Threads
- agregar valores a una tablea de SQL
- [RODBC] date attribute in sqlQuery
- Object is populated but attributes are unavailable
- Yum update changes inode of file
- HOWTO: A simple AGI application to modify incomi ng CallerID on the fly using SQL Server and *not* UnixODBC