diff --git a/lib/her/model/associations/association.rb b/lib/her/model/associations/association.rb index 6a4ae110..228b54c5 100644 --- a/lib/her/model/associations/association.rb +++ b/lib/her/model/associations/association.rb @@ -50,9 +50,13 @@ def fetch(opts = {}) return @cached_result unless @params.any? || @cached_result.nil? return @parent.attributes[@name] unless @params.any? || @parent.attributes[@name].blank? - path = build_association_path lambda { "#{@parent.request_path(@params)}#{@opts[:path]}" } - @klass.get(path, @params).tap do |result| - @cached_result = result unless @params.any? + if @params.values.include?([]) and !self.is_a?(HasOneAssociation) + Her::Collection.new + else + path = build_association_path lambda { "#{@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 2daa80b9..3d537ea6 100644 --- a/lib/her/model/relation.rb +++ b/lib/her/model/relation.rb @@ -65,10 +65,14 @@ def kind_of?(thing) # @private def fetch @_fetch ||= begin - path = @parent.build_request_path(@params) - method = @parent.method_for(:find) - @parent.request(@params.merge(:_method => method, :_path => path)) do |parsed_data, response| - @parent.new_collection(parsed_data) + if @params.values.include?([]) + Her::Collection.new + else + path = @parent.build_request_path(@params) + method = @parent.method_for(:find) + @parent.request(@params.merge(:_method => method, :_path => path)) do |parsed_data, response| + @parent.new_collection(parsed_data) + end end end end diff --git a/spec/model/associations_spec.rb b/spec/model/associations_spec.rb index fc952721..9edf8029 100644 --- a/spec/model/associations_spec.rb +++ b/spec/model/associations_spec.rb @@ -182,6 +182,13 @@ @user_without_included_data.comments.first.object_id.should_not == @user_without_included_data.comments.where(:foo_id => 1).first.object_id end + it "returns an empty collection without fetching when [] is a parameter" do + Foo::Comment.should_not_receive(:request) + comments = @user_without_included_data.comments.where(:foo_id => []) + comments.should respond_to(:length) + comments.size.should eql 0 + end + it "maps an array of included data through has_one" do @user_with_included_data.role.should be_a(Foo::Role) @user_with_included_data.role.object_id.should == @user_with_included_data.role.object_id diff --git a/spec/model/relation_spec.rb b/spec/model/relation_spec.rb index 4c166700..fd4d535d 100644 --- a/spec/model/relation_spec.rb +++ b/spec/model/relation_spec.rb @@ -51,6 +51,13 @@ Foo::User.create(:fullname => 'George Michael Bluth').id.should == 3 Foo::User.all.size.should 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