I am trying to move my user account and session data to a separate database so that we can end up sharing it in several applications.
I saw a lot of people on the net talking to use establish_connectionto tell models to connect to another db, but I can't get this to work.
config /database.yml
development:
adapter: mysql2
encoding: utf8
reconnect: true
pool: 5
host: localhost
database: project_name_development
authentication:
adapter: mysql2
encoding: utf8
reconnect: true
pool: 5
host: localhost
database: authentication
application / models / user.rb
class User < ActiveRecord::Base
establish_connection :authentication
has_one :person
end
application / models / person.rb
class Person < ActiveRecord::Base
belongs_to :user
end
This seems to work:
> User.connection.instance_eval { @config[:database] }
=> "authentication"
> Person.connection.instance_eval { @config[:database] }
=> "project_name_development"
and I can request Userindividually:
> User.where(:admin => true)
=> [ ... lots of results .. ]
but as soon as I try to use join, it breaks:
> User.joins(:person)
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'authentication.people' doesn't exist: SELECT `users`.* FROM `users` INNER JOIN `people` ON `people`.`user_id` = `users`.`id`
It seems that AREL is using the current database instead of getting the right reflection.
, , ARel, , .
?
: :
User.joins("INNER JOIN project_name.people ON project_name.people.user_id = authentication.users.id")
, , , .
:
set_table_name 'project_name.people'
NoMethodError: undefined method `eq' for nil:NilClass
, Rails3 . ?