Forum: Ruby on Rails Column name becames invalid after LEFT join

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Wai T. (Guest)
on 2007-02-26 23:46
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?
Bala P. (Guest)
on 2007-02-27 02:38
(Received via mailing list)
Is 'application' a column in one of your tables? Maybe you are using
the wrong column name in the conditions.
Wai T. (Guest)
on 2007-02-27 23:36
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 :p

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?
This topic is locked and can not be replied to.