2

私は Rails 3.2 アプリケーションを持っており、多くのクライアントと 1 つのアプリケーションに対して 1 つのデータベースを使用したいと考えています。したがって、すべてのモデルに対して というフィールドを作成しました。今度は、ロギング ユーザーaccount_idのベースで行をフィルタリングするためのグローバル スコープを追加します (セッション パラメータです)。したがって、初期化でファイルを作成し、これらのコードを配置しましたaccount_idaccount_id

module ActiveRecord
  # = Active Record Named \Scopes                                                                                                                                                                                 \

  module Scoping
    module Default
      module ClassMethods

        def unscoped #:nodoc:                                                                                                                                                         
            return  (block_given? ? relation.scoping { yield } : relation).where(account_id: Thread.current['account_id'].id)

    end

        def default_scope(scope = {})
          scope = Proc.new if block_given?
          if scope.kind_of?(Array) || scope.is_a?(Hash)
              scope.merge!({:conditions=>{account_id:Thread.current['account_id'].id}})
            end
            self.default_scopes = default_scopes + [scope]
          end
        end
   end
  end
end

ユーザーでログインした場合account_id=2はすべて問題ありませんが、同時に別のブラウザーまたはコンピューターにログインした場合account_id=3...多くのエラーがあり、ログにアプリケーションaccount_id=2が同時に使用されていることがわかりaccount_id=3ました。

解決策はありますか?どうすれば書き直せdefault_scope(scope = {})ますか?他のアイデア?

4

1 に答える 1