diff --git a/lib/her/model/associations/association.rb b/lib/her/model/associations/association.rb index 3548dc80..daf1cc7c 100644 --- a/lib/her/model/associations/association.rb +++ b/lib/her/model/associations/association.rb @@ -48,9 +48,13 @@ def fetch(opts = {}) return @parent.attributes[@name] unless @params.any? || @parent.attributes[@name].blank? return @opts[:default].try(:dup) if @parent.new? - path = build_association_path -> { "#{@parent.request_path(@params)}#{@opts[:path]}" } - @klass.get(path, @params).tap do |result| - @cached_result = result unless @params.any? + if @params.values.include?([]) && !is_a?(HasOneAssociation) + Her::Collection.new + else + path = build_association_path -> { "#{@parent.request_path(@params)}#{@opts[:path]}" } + @klass.get(path, @params).tap do |result| + @cached_result = result unless @params.any? + end end end diff --git a/lib/her/model/relation.rb b/lib/her/model/relation.rb index df476392..b6a93b9e 100644 --- a/lib/her/model/relation.rb +++ b/lib/her/model/relation.rb @@ -67,10 +67,14 @@ def kind_of?(thing) # @private def fetch @_fetch ||= begin - path = @parent.build_request_path(@parent.collection_path, @params) - method = @parent.method_for(:find) - @parent.request(@params.merge(:_method => method, :_path => path)) do |parsed_data, _| - @parent.new_collection(parsed_data) + if @params.values.include?([]) + Her::Collection.new + else + path = @parent.build_request_path(@parent.collection_path, @params) + method = @parent.method_for(:find) + @parent.request(@params.merge(:_method => method, :_path => path)) do |parsed_data, _| + @parent.new_collection(parsed_data) + end end end end diff --git a/spec/model/associations_spec.rb b/spec/model/associations_spec.rb index a6632fa4..43d213bd 100644 --- a/spec/model/associations_spec.rb +++ b/spec/model/associations_spec.rb @@ -440,6 +440,13 @@ expect(user.organization.respond_to?(:empty?)).to be_truthy expect(user.organization).not_to be_empty end + + it "returns an empty collection without fetching when [] is a parameter" do + Foo::Comment.should_not_receive(:request) + comments = user.comments.where(:foo_id => []) + comments.should respond_to(:length) + comments.size.should eql 0 + end end context "without included parent data" do diff --git a/spec/model/relation_spec.rb b/spec/model/relation_spec.rb index b8bd658c..9f2fc665 100644 --- a/spec/model/relation_spec.rb +++ b/spec/model/relation_spec.rb @@ -60,6 +60,13 @@ expect(Foo::User.create(fullname: "George Michael Bluth").id).to eq(3) expect(Foo::User.all.size).to eql 3 end + + it "returns an empty collection without fetching when [] is given" do + Foo::User.should_not_receive(:request) + users = Foo::User.where(:fullname => []) + users.should respond_to(:length) + users.size.should eql 0 + end end context "for parent class" do