# Column name becames invalid after LEFT join

**URL:** <https://www.ruby-forum.com/t/column-name-becames-invalid-after-left-join/86925>\
**Category:** Rails\
**Created:** [February 26, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/column-name-becames-invalid-after-left-join/86925 "2007-02-26T22:46:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 26, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/column-name-becames-invalid-after-left-join/86925/1 "2007-02-26T22:46:09Z")

</div>

Hi,

I was adding a new relationship to my database tables. Basically,  
previously an application has many hosts, so the paginate statement  
would be like this when I am listing hosts for abc application:

@hosts\_pages, @hosts = paginate(:hosts, :include =\> :app, :conditions =\>  
“application = abc”)

model:  
App  
has\_many :hosts, :dependent =\> :destroy  
Host  
belongs\_to :app

It works fine so far.

Now, I am adding a role table and it no longer works.

@hosts\_pages, @hosts = paginate(:hosts, :include =\> [:app, :role],  
:conditions =\> “application = abc”)

model:  
App  
has\_many :hosts, :dependent =\> :destroy  
Host  
belongs\_to :app  
has\_many :host\_permissions, :dependent =\> :destroy  
has\_many :role, :through =\> :host\_permissions  
Role  
has\_many :host\_permissions, :dependent =\> :destroy  
has\_many :host, :through =\> :host\_permissions

It would complain

Unknown column ‘application’ in ‘where clause’: SELECT id FROM hosts  
WHERE (application = ‘abc’) LIMIT 0, 10

It doesn’t make any sense since adding a left outer join statement will  
only add columns, not remove them. Do anyone have any suggestion what I  
have done wrong?

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 27, 2007, 1:38am UTC](https://www.ruby-forum.com/t/column-name-becames-invalid-after-left-join/86925/2 "2007-02-27T01:38:38Z")

</div>

Is ‘application’ a column in one of your tables? Maybe you are using  
the wrong column name in the conditions.

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 27, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/column-name-becames-invalid-after-left-join/86925/3 "2007-02-27T22:36:07Z")

</div>

Bala P. wrote:

> Is ‘application’ a column in one of your tables? Maybe you are using  
> the wrong column name in the conditions.

Of course. Otherwise, it wouldn’t work before 😛

I found a workaround for this problem. It seems like if :include is  
used, and if more than one table is specified, the column name can only  
be matched if you specify the table name as well. In my case, it would  
be “apps.application”. Maybe it is a bug in rails?
