Column name becames invalid after LEFT join

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?

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

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 :stuck_out_tongue:

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?