实现查找某个用户的所有好友
user model 的代码
class System::User < System::BaseDb
self.abstract_class = true
establish_connection :system
self.table_name = "system.users"
has_many :friend_relationships
has_many :friends, through: :friend_relationships, source: :user
end
friend_relationships model 的代码
class FriendRelationship < ActiveRecord::Base
belongs_to :user, :class_name => "System::User"
belongs_to :friend, :class_name => "System::User"
end
这样配置是会报错的
Mysql2::Error: Table 'system.friend_relationships' doesn't exist
在 FriendRelationship 这个 model 中加上一行代码能解决这个问题
class FriendRelationship < ActiveRecord::Base
self.table_name = "thalassa.friend_relationships"
belongs_to :user, :class_name => "System::User"
belongs_to :friend, :class_name => "System::User"
end