# Rails 2.0.2 - quoted\_table\_name problem

**URL:** <https://www.ruby-forum.com/t/rails-2-0-2-quoted-table-name-problem/127103>\
**Category:** Rails\
**Created:** [January 24, 2008, 11:49pm UTC](https://www.ruby-forum.com/t/rails-2-0-2-quoted-table-name-problem/127103 "2008-01-24T23:49:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kxkannan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b9e5f3/32.png) [@kxkannan](https://www.ruby-forum.com/u/kxkannan)\
**Post date:** [January 24, 2008, 11:49pm UTC](https://www.ruby-forum.com/t/rails-2-0-2-quoted-table-name-problem/127103/1 "2008-01-24T23:49:21Z")

</div>

Hello,

I am trying to upgrade existing Rails codebase from Rails 1.2.5 to  
Rails 2.0.2. My db is mysql.

I ran into a “table not found” error after upgrading in one of the test  
cases which works perfectly under 1.2.5.

So this assert statement gives the following error :

assert\_equal(43, QciQuestion.find(:all,  
:joins =\> "as qq inner join qcis as qc on qq.qci\_id = qc.id ",  
:conditions =\> [“qc.unit\_id = ?”, 1]).size, “Wrong number of  
resident interview questions loaded.”)

## Error

ActiveRecord::StatementInvalid: Mysql::Error: #42S02Unknown table  
‘qci\_questions’: SELECT `qci_questions`.\* FROM `qci_questions` as qq  
inner join qcis as qc on qq.qci\_id = qc.id WHERE (qc.unit\_id = 1)

So, I was debugging and ran into the “construct\_finder\_sql” method in  
ActiveRecord. This method adds a quoted table name in front of the “.\*”  
if there a “join” option is specified. Apparently this is what causing  
the Unknown table error above.

## Rails 2.0.2 ActiveRecord::Base

def construct\_finder\_sql(options)  
scope = scope(:find)  
sql = "SELECT #{(scope && scope[:select]) || options[:select]  
|| (options[:joins] && quoted\_table\_name + ‘._') || '_’} "  
sql \<\< "FROM #{(scope && scope[:from]) || options[:from] ||  
quoted\_table\_name} "

…  
…  
end

## Rails 1.2.5 ActiveRecord::Base

def construct\_finder\_sql(options)  
scope = scope(:find)  
sql = "SELECT #{(scope && scope[:select]) || options[:select]  
|| ‘\*’} "  
sql \<\< "FROM #{(scope && scope[:from]) || options[:from] ||  
table\_name} "  
…  
…  
end

## Fix

To fix this, I had to provide a “select =\> _" option to the finder so  
the code doesn’t add the quoted\_table\_name to the "._”.

## Questions:

1. Is there any option to suppress adding of this quoted\_table\_name to  
the query  
?

2. I have pored over the release notes (kind of) on [rubyonrails.org](http://rubyonrails.org),  
this forum and other references to Rails 2.0.2 and could never find any  
mention about this. Did anyone see any documentation related to this  
change ?

3. Has anyone run into this problem and solved it differently ?

Thanks,  
Kannan

---

<div class="post-metadata">

**Author:** ![kxkannan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b9e5f3/32.png) [@kxkannan](https://www.ruby-forum.com/u/kxkannan)\
**Post date:** [January 25, 2008, 2:47am UTC](https://www.ruby-forum.com/t/rails-2-0-2-quoted-table-name-problem/127103/2 "2008-01-25T02:47:48Z")

</div>

Sorry, but could you explain what you’re trying to do with the join  
statement there? all I see is qq and qcs… I need it written in english  
for  
me to understand it.

On Jan 25, 2008 9:19 AM, Krishnan Kannan  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ‘qci\_questions’: SELECT `qci_questions`.\* FROM `qci_questions` as qq
> 
> …  
> Questions:  
> 3. Has anyone run into this problem and solved it differently ?
> 
> ## Thanks, Kannan
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![kxkannan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b9e5f3/32.png) [@kxkannan](https://www.ruby-forum.com/u/kxkannan)\
**Post date:** [January 25, 2008, 12:59pm UTC](https://www.ruby-forum.com/t/rails-2-0-2-quoted-table-name-problem/127103/3 "2008-01-25T12:59:41Z")

</div>

On 24 Jan 2008, at 22:49, Krishnan Kannan wrote:

> So this assert statement gives the following error :
> 
> assert\_equal(43, QciQuestion.find(:all,  
> :joins =\> "as qq inner join qcis as qc on qq.qci\_id = qc.id ",  
> :conditions =\> [“qc.unit\_id = ?”, 1]).size, “Wrong number of  
> resident interview questions loaded.”)

basically AR isn’t expecting you to alias the main table you’re  
querying (and short of being able to parse the joins statement, how  
could it?).  
Wild speculation, but i suspect the reason it does this is because  
previously you’d very commonly do

Foo.find :all, :joins =\> ‘…’  
and then you spend 5 minutes scratching your head because weird stuff  
is happening because the id column from foos is getting squashed by  
the id column for this join tables. so you add :select =\> ‘foos._’ to  
the thing. The change means that in the 95% of cases where you where  
just systematically adding on :select =\> 'foos._’ you don’t have to.

Fred

---

<div class="post-metadata">

**Author:** ![kxkannan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b9e5f3/32.png) [@kxkannan](https://www.ruby-forum.com/u/kxkannan)\
**Post date:** [January 25, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/rails-2-0-2-quoted-table-name-problem/127103/4 "2008-01-25T17:17:40Z")

</div>

Hello Ryan,

I can explain what’s the qq and qcs but my feeling is that’s not what is  
causing the problem.

Basically there are 2 tables, Qcis (table qcis) and QciQuestion (table  
qci\_questions). qci\_questions is a child of qcis table using qci\_id as  
the foreign key. qcis table has an id column as it’s primary key and  
also have the Unit column.

In plain english this query translates to  
“Get me all the qci\_questions where the unit id is 1 in qcis”

The qq,qc are alias for the table qci\_question and table qcis,  
respectively.

Hope this helps.

Kannan

Ryan B. wrote:

> Sorry, but could you explain what you’re trying to do with the join  
> statement there? all I see is qq and qcs… I need it written in english  
> for  
> me to understand it.
> 
> On Jan 25, 2008 9:19 AM, Krishnan Kannan  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ‘qci\_questions’: SELECT `qci_questions`.\* FROM `qci_questions` as qq
> > 
> > …  
> > Questions:  
> > 3. Has anyone run into this problem and solved it differently ?
> > 
> > ## Thanks, Kannan
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> Ryan B.  
> [http://www.frozenplague.net](http://www.frozenplague.net)  
> Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![kxkannan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b9e5f3/32.png) [@kxkannan](https://www.ruby-forum.com/u/kxkannan)\
**Post date:** [January 25, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/rails-2-0-2-quoted-table-name-problem/127103/5 "2008-01-25T17:20:45Z")

</div>

Frederick,

If your assumption is correct (alias the main table), is that a change  
in Rails 2.0 (AR in Rails 2.0 specifically) ? Because this query worked  
as such in Rails 1.2.5. As I have shown the AR code in my posting, the  
quoted\_table\_name is something new in AR for Rails 2.0.2 and I am trying  
to understand why it was added and if there is an option to not apply  
it.

Thanks,  
Kannan

Frederick C. wrote:

> On 24 Jan 2008, at 22:49, Krishnan Kannan wrote:
> 
> > So this assert statement gives the following error :
> > 
> > assert\_equal(43, QciQuestion.find(:all,  
> > :joins =\> "as qq inner join qcis as qc on qq.qci\_id = qc.id ",  
> > :conditions =\> [“qc.unit\_id = ?”, 1]).size, “Wrong number of  
> > resident interview questions loaded.”)
> 
> basically AR isn’t expecting you to alias the main table you’re  
> querying (and short of being able to parse the joins statement, how  
> could it?).  
> Wild speculation, but i suspect the reason it does this is because  
> previously you’d very commonly do
> 
> Foo.find :all, :joins =\> ‘…’  
> and then you spend 5 minutes scratching your head because weird stuff  
> is happening because the id column from foos is getting squashed by  
> the id column for this join tables. so you add :select =\> ‘foos._’ to  
> the thing. The change means that in the 95% of cases where you where  
> just systematically adding on :select =\> 'foos._’ you don’t have to.
> 
> Fred

---

<div class="post-metadata">

**Author:** ![kxkannan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b9e5f3/32.png) [@kxkannan](https://www.ruby-forum.com/u/kxkannan)\
**Post date:** [January 28, 2008, 4:32pm UTC](https://www.ruby-forum.com/t/rails-2-0-2-quoted-table-name-problem/127103/6 "2008-01-28T16:32:23Z")

</div>

No change of which I am aware.

A couple of alternatives, though:

1. Why not try a find\_by\_sql here? It looks like you are trying too  
hard to use the generic #find and force feed it a very specific join.
2. Is there a reason that you cannot use find with the :include  
option, since it appears that you’re trying to check the size of the  
associated array?
3. Is the test in the right place? It seems that you are really  
testing an attribute of Qcis (the number of questions associated to  
it) rather than an attribute/state of the QciQuestion itself.

Regards,  
AndyV

On Jan 25, 11:20 am, Krishnan Kannan \<rails-mailing-l…@andreas-
