diff --git a/lib/cancan/model_adapters/active_record_adapter.rb b/lib/cancan/model_adapters/active_record_adapter.rb index 3cc55a4b..74cc5e23 100644 --- a/lib/cancan/model_adapters/active_record_adapter.rb +++ b/lib/cancan/model_adapters/active_record_adapter.rb @@ -52,7 +52,7 @@ def self.meta_where_match?(subject_value, method, value) # query(:manage, User).conditions # => "not (self_managed = 't') AND ((manager_id = 1) OR (id = 1))" # def conditions - if @rules.size == 1 && @rules.first.base_behavior + if (@rules.size == 1 || @rules.uniq{|r| r.conditions}.size == 1) && @rules.first.base_behavior # Return the conditions directly if there's just one definition tableized_conditions(@rules.first.conditions).dup else @@ -87,7 +87,7 @@ def joins def database_records if override_scope - @model_class.scoped.merge(override_scope) + @model_class.all.merge(override_scope) elsif @model_class.respond_to?(:where) && @model_class.respond_to?(:joins) mergeable_conditions = @rules.select {|rule| rule.unmergeable? }.blank? if mergeable_conditions diff --git a/lib/cancan/rule.rb b/lib/cancan/rule.rb index a3c86ef4..36e0fbc2 100644 --- a/lib/cancan/rule.rb +++ b/lib/cancan/rule.rb @@ -39,7 +39,7 @@ def matches_conditions?(action, subject, attribute) matches_conditions_hash?(subject) else # Don't stop at "cannot" definitions when there are conditions. - @conditions.empty? ? true : @base_behavior + (@conditions.is_a?(ActiveRecord::Relation) || @conditions.empty?) ? true : @base_behavior end end @@ -48,7 +48,7 @@ def only_block? end def only_raw_sql? - @block.nil? && conditions? && !@conditions.kind_of?(Hash) + @block.nil? && conditions? && !@conditions.kind_of?(Hash) && !@conditions.is_a?(ActiveRecord::Relation) end def attributes? @@ -56,11 +56,11 @@ def attributes? end def conditions? - @conditions.present? + @conditions.is_a?(ActiveRecord::Relation) || @conditions.present? end def instance_conditions? - @block || conditions? + @block || (!@conditions.is_a?(ActiveRecord::Relation) && conditions?) end def unmergeable?