On Apr 13, 2010, at 10:23 PM, tispratik wrote:

Has anyone faced this problem before?

I have 3 models, Project, ProjectRole and User. The tables are
distributed in two databases.

--------------------------------------------------------------------------------------------
Databases:
--------------------------------------------------------------------------------------------
Database X:
 users

Database Y:
 projects
 project_roles

--------------------------------------------------------------------------------------------
Models:
---------------------------------------------------------------------------------------------
class Project < ActiveRecord::Base
 has_many :project_roles
 has_many :users, :through => :project_roles
end

class ProjectRole < ActiveRecord::Base
 belongs_to :project
 belongs_to :user
end

class User < ActiveRecord::Base
 establish_connection :X
 has_many :project_roles
 has_many :projects, :through => :project_roles
end
----------------------------------------------------------------------------------------------

Now when i try to access users from project:

p = Project.first
+----+-----------+-------+
| id | name     | alias |
+----+-----------+-------+
| 1  | Jerde L  | PRO |
+----+-----------+-------+

p.users
Hirb Error: Mysql::Error: Table 'X.project_roles' doesn't exist:
SELECT `users`.* FROM `users`  INNER JOIN `project_roles` ON
`users`.id = `project_roles`.user_id    WHERE
((`project_roles`.project_id = 1))
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/connection_adapters/abstract_adapter.rb:219:in `log'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/connection_adapters/mysql_adapter.rb:323:in `execute'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/connection_adapters/mysql_adapter.rb:608:in `select'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/connection_adapters/abstract/database_statements.rb:7:in
`select_all_without_query_cache'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/connection_adapters/abstract/query_cache.rb:62:in
`select_all'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/base.rb:661:in `find_by_sql'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/base.rb:1548:in `find_every'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/base.rb:615:in `find'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/associations/has_many_through_association.rb:83:in
`find_target'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/associations/association_collection.rb:354:in
`load_target'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/associations/association_proxy.rb:212:in
`method_missing'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/associations/association_collection.rb:371:in
`method_missing_without_paginate'
   c:/rubyonrails/rails_apps/test_project/vendor/gems/
will_paginate-2.3.12/lib/will_paginate/finder.rb:170:in
`method_missing'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/hirb-0.3.1/lib/hirb/
formatter.rb:78:in `determine_output_class'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/hirb-0.3.1/lib/hirb/
formatter.rb:48:in `format_output'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/hirb-0.3.1/lib/hirb/
view.rb:213:in `render_output'
   C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/hirb-0.3.1/lib/hirb/
view.rb:126:in `view_output'



BUT: Getting projects from users works just fine.

u = User.first
+----+--------------+
| id | username |
+----+--------------+
| 1  | dummy     |
+----+---------------+

u.projects
+----+---------------------+-------+
| id | name               | alias |
+----+---------------------+--------+
| 1  | Jerde L            | PRO  |
| 2  | Spinka Group   | PRO  |
| 3  | Mosciski LLC   | PRO  |
+---+-----------------------+-------+


Any idea what could be wrong?

You're trying to run a single SQL query that references tables in two different databases. That isn't going to work. Set things up so that you can see the SQL being run and you'll see why the first isn't working and the second is. The second isn't trying to join any tables. It's running a query on the second database using a *value* retrieved from the first.

-philip

--
You received this message because you are subscribed to the Google Groups "Ruby on 
Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to