rails/rails — Issue #3595: [ActiveRecord] Add a condition on the join table make multiple SHOW TABLES calls
Author: noefroidevaux
Created: 2011-11-10 11:15:47+00:00
State: open

Hi,

For example, if we have a Document model with a parent_id and privacy attributes (with "belongs_to :parent, :foreign_key => 'parent_id', :class_name => 'Document'"), I try to have a query like:

```ruby
Document.joins(:parent).where("parents_documents.privacy = ?", 1)
```

Every time I run this query, a call is made on database with "SHOW TABLES", because ActiveRecord don't cache the table which is not in database.

In ConnectionPool (lib/active_record/connection_adapters/abstract/connection_pool.rb), currently, we have:

```ruby
def table_exists?(name)
return true if @tables.key? name

with_connection do |conn|
conn.tables.each { |table| @tables[table] = true }
@tables[name] = true if !@tables.key?(name) && conn.table_exists?(name)
end

@tables.key? name
end
```

I propose to change to:

```ruby
def table_exists?(name)
return @tables[name] if @tables.key? name

with_connection do |conn|
conn.tables.each { |table| @tables[table] = true }
@tables[name] = !@tables.key?(name) && conn.table_exists?(name) ? true : false
end

@tables[name] if @tables.key? name
end
```

What do you think? It is the right way?