This project is archived and is in readonly mode.
problem joining two models that establish_connection to the same db
-
Miles Egan
It looks to me like the issue aries in Arel at this point:
lib/arel/algebra/relations/operations/join.rb, line 44:
def engine
relation1.engine != relation2.engine ? Memory::Engine.new : relation1.engine endEven though the two engines are pointing to the same underlying data source, they don't compare as equal because they're different objects.
-
Miles Egan
Digging a little deeper, it looks like rails maintains a distinct connection pool for each class that establishes a connection. In most cases there's only one since it's established in ActiveRecord::Base but since you're establishing connections in two separate classes two separate connection pools are created and Arel doesn't recognize them as speaking to the same DB.
If you read the comments in /activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb around the ConnectionHandler class it explains the relationship between connection pools and classes in more detail.
It looks to me like fixing this would require a fairly significant change in the connection pooling code.
-
Albert Llop Yacobi
This bug hit me today aswell. Managed it changing all of the queries to find_by_sql, but it's a shame losing all the ActiveRecord goodness. Any progress on the matter?
-
Kane
If you want a tempory fix till this issue is addressed in the rails core you could make a superclass for this models in which you connect to the database.
class Bugzilla < ActiveRecord::Base self.abstract_class = true establish_connection :bugzilla end class Bug < Bugzilla set_primary_key :bug_id has_many :flags end class Flag < Bugzilla belongs_to :bug end -
ronin-68998 (at lighthouseapp)
By Kane: If you want a tempory fix till this issue is addressed in the rails core you could make a superclass for this models in which you connect to the database.
This man is genius, flawless hack. Thanks.
I'm using ActiveScaffold w/ Rails 3, and a remote database, ran into this bug when I started to join models together.
Hit: undefined method 'connection' in Arel::Memory, bleh bleh bleh.
Thanks again.
-
rails
- State changed from new to open
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
rails
- State changed from open to stale
-
ravionrails
- State changed from stale to open
Address model
establish_connection :connection_name (mention in database.yml)If I do
User.joins(:addresses)
it doesn't connect to the addresses table of other db mention in :connection_name
but try to find the addresses table in the application's databaseit ignores the establish_connection
but joins work on tables which are in the application's (default) database
I think there is problem in joins for establish_connection models/tables
[state:open]