1

私はこのようなことで考えます:

def self.obj_list(opts = {:include => [] , :exclude => []})
    # Returns an array with all objects with roles applied
    # +:exclude+:: (array,string) optional object type to exclude from list
    # +:include+:: (array,string) optional object type to include in list
    # Example:
    #   Role.obj_list(:include => ["Device", "User"])
    #   Role.obj_list(:exclude => ["User"])

    inc = opts[:include].to_a
    exc = opts[:exclude].to_a

    objs = []
    if inc.empty?

      self.all.each do |r|
        unless r.authorizable_type.nil?
          objs << r.authorizable_type.constantize.find(r.authorizable_id) unless exc.include?(r.authorizable_type)
        end
      end

    else

      self.all.each do |r|
        unless r.authorizable_type.nil?
          objs << r.authorizable_type.constantize.find(r.authorizable_id) if inc.include?(r.authorizable_type)
        end
      end

    end
    objs
  end
4

2 に答える 2

0

わかりません。オブジェクトとサブジェクトをリンクさせたいのでしょうか?これである場合は、そのためのチュートリアルがあります: https://github.com/be9/acl9/wiki/tutorial:-linking-object-and-subject-with-hmt

于 2010-06-22T22:18:09.623 に答える
0

句を使用whereして、SQL で包含/除外を行うことができます。

( inc.empty?
? where.not( :authorizable_type => exc )
: where( :authorizable_type => inc )
).map(&:authorizable)

を使用authorizableすると、Rails 独自のポリモーフィック アソシエーションの処理が得られます。これにより、実際のオブジェクトのみが返されることが保証されるため、チェックする必要はありません。nil

于 2015-01-22T21:36:37.350 に答える