# Problems with ActiveRecord, Oracle adapter, find\_by\_sql, mul

**URL:** <https://www.ruby-forum.com/t/problems-with-activerecord-oracle-adapter-find-by-sql-mul/69367>\
**Category:** Rails\
**Created:** [September 20, 2006, 4:52pm UTC](https://www.ruby-forum.com/t/problems-with-activerecord-oracle-adapter-find-by-sql-mul/69367 "2006-09-20T16:52:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [September 20, 2006, 4:52pm UTC](https://www.ruby-forum.com/t/problems-with-activerecord-oracle-adapter-find-by-sql-mul/69367/1 "2006-09-20T16:52:59Z")

</div>

Hi all,

Windows XP  
Oracle 10g client  
OCI8 0.1.16  
Ruby 1.8.5  
Rails 1.1.6

I’m hitting weird behavior with the Oracle adapter and a find\_by\_sql  
call on a multi-table join.

The SQL looks like this:

SELECT DISTINCT e1.ip\_address, loc.street, loc.city, loc.state  
FROM schema.net\_element ne, schema.equipment e1, schema.equipment e2,  
schema.building b, schema.cust\_loc cl, schema.location loc  
WHERE e2.equipment\_id = e1.equipment\_id  
AND e2.some\_column like ‘SOME\_VAL%’  
AND e1.equipment\_id = ne.net\_element\_id  
AND ne.class\_name = ‘FooBar’  
AND e1.building\_id = b.building\_id  
AND b.main\_address = cl.cust\_loc\_id  
AND cl.location\_id = loc.location\_id

So, I setup a class for the ‘Location’ table like this:

class OracleLocation \< ActiveRecord::Base  
establish\_connection(  
:adapter =\> “oracle”,  
:database =\> “our\_db”,  
:username =\> “user”,  
:password =\> “password”  
)

set\_table\_name :location  
set\_primary\_key “location\_id”  
end

I can get a record back just fine:

loc = OracleLocation.find\_by\_sql(sql).first  
p loc

#\<OracleLocation:0x385c610 @attributes={“state”=\>“WA”, “street”=\>“1234  
Block St”, “city”=\>“Smallville”, “ip\_address”=\>“172.1.2.3”}\>

However, if I try to refer to an attribute, either directly or via the  
‘attributes’ method, I get an error:

p loc.attributes # Boom!

(eval):3:in ` __send__': ORA-04043: object location does not exist (OCIError) from (eval):3:in`describe’  
from  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/oracle\_adapter.rb:  
361:in `columns' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:696:in`columns’  
from  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/base.rb:704:in  
`columns_hash' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1562:in`column\_for\_attribute’

```
    from

```

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/base.rb:1801:in  
`read_attribute' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:2067:in`send’  
from  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/base.rb:2067:in  
`clone_attribute_value ' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:2061:in`clone\_attributes’  
from  
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/inflector.rb:161:in  
`inject' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:2060:in`each’  
from  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/base.rb:2060:in  
`inject' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:2060:in`clone\_attributes’  
from  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/base.rb:1519:in  
`attributes' from oracletest2.rb:35:in`send’  
from oracletest2.rb:35

This error confuses me. There clearly _is_ a location table, or the  
query wouldn’t work in the first place. Also, the fact that I’m  
getting back an ORA-04043 indicates to me that, when I call  
loc.attributes, it’s calling out to the database _again_ for some  
reason. Why?

Is this error caused by the fact that I’m doing a multi-table join  
here, but haven’t actually mapped any of the other tables besides  
Location? What’s going on?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [September 21, 2006, 6:36am UTC](https://www.ruby-forum.com/t/problems-with-activerecord-oracle-adapter-find-by-sql-mul/69367/2 "2006-09-21T06:36:40Z")

</div>

> FROM schema.net\_element ne, schema.equipment e1, schema.equipment e2,  
> schema.building b, schema.cust\_loc cl, schema.location loc

> set\_table\_name :location

> This error confuses me. There clearly _is_ a location table, or the  
> query wouldn’t work in the first place. Also, the fact that I’m  
> getting back an ORA-04043 indicates to me that, when I call  
> loc.attributes, it’s calling out to the database _again_ for some  
> reason. Why?

The problem is that your “Location” table appears to be in the schema  
“schema”, but you’re setting the table name to just “location”.

Try:

```
set_table_name 'schema.location'
```
