# Find\_by\_sql not as cool as AR?

**URL:** <https://www.ruby-forum.com/t/find-by-sql-not-as-cool-as-ar/68370>\
**Category:** Rails\
**Created:** [September 12, 2006, 4:03am UTC](https://www.ruby-forum.com/t/find-by-sql-not-as-cool-as-ar/68370 "2006-09-12T04:03:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [September 12, 2006, 4:03am UTC](https://www.ruby-forum.com/t/find-by-sql-not-as-cool-as-ar/68370/1 "2006-09-12T04:03:38Z")

</div>

I need to do a few custom SQL join’s

so was mucking around in the console

AR stuff works fine when trying to access the instance.id

@tile = Tile.find(:first, :conditions =\> ["id = ? ", 10])

> > @tile.id  
> > =\> 10

but doing a custom SQL query I get this when trying to look at  
instance.id

# @tile = Tile.find\_by\_sql("select id, blog\_name FROM tiles WHERE tiles.id

10")

> > @tile.id  
> > (irb):85: warning: Object#id will be deprecated; use Object#object\_id  
> > =\> 29652588

I have seen a few posts which say to do stuff like to access the object  
id  
instead of the ruby id

class \<\< ActiveRecord  
alias “id\_\_”, “id”  
end

but this seems uncool to me

can anyone (DHH) explain whats going on here???

cheers heaps

dion

–

[www.blogsaic.com](http://www.blogsaic.com)  
search, post, blog

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [September 12, 2006, 4:09am UTC](https://www.ruby-forum.com/t/find-by-sql-not-as-cool-as-ar/68370/2 "2006-09-12T04:09:38Z")

</div>

> @tile = Tile.find\_by\_sql (“select id, blog\_name FROM tiles WHERE tiles.id =  
> 10”)
> 
> > > @tile.id  
> > > (irb):85: warning: Object#id will be deprecated; use Object#object\_id  
> > > =\> 29652588

I’m guessing that @tile is an array. You probably want this instead:

@tile = Tile.find\_by\_sql ("select id, blog\_name FROM tiles WHERE  
tiles.id =

> 10").first

If you need help, just do something like this:

@tile.class

That’ll tell you what you’re dealing with.

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [September 12, 2006, 4:13am UTC](https://www.ruby-forum.com/t/find-by-sql-not-as-cool-as-ar/68370/3 "2006-09-12T04:13:39Z")

</div>

find\_by\_sql returns an array, as it works like find(:all)

if you were to do @tile.class you would get Array

Chris

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [September 12, 2006, 4:23am UTC](https://www.ruby-forum.com/t/find-by-sql-not-as-cool-as-ar/68370/4 "2006-09-12T04:23:40Z")

</div>

sorry about that

I was being a dick

I was using some code from my pagination stuff

and fogot to loop through the array like I normally do

\<% for row in @tiles %\>

cheers

On 9/12/06, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

–

[www.blogsaic.com](http://www.blogsaic.com)  
search, post, blog
