diff --git a/lib/her/model/parse.rb b/lib/her/model/parse.rb index db43d98f..a4d67472 100644 --- a/lib/her/model/parse.rb +++ b/lib/her/model/parse.rb @@ -58,7 +58,7 @@ def to_params(attributes, changes={}) def embeded_params(attributes) associations[:has_many].select { |a| attributes.include?(a[:data_key])}.compact.inject({}) do |hash, association| params = attributes[association[:data_key]].map(&:to_params) - next if params.empty? + next {} if params.empty? if association[:class_name].constantize.include_root_in_json? root = association[:class_name].constantize.root_element hash[association[:data_key]] = params.map { |n| n[root] } diff --git a/spec/model/associations_spec.rb b/spec/model/associations_spec.rb index a3bf3f68..696176a7 100644 --- a/spec/model/associations_spec.rb +++ b/spec/model/associations_spec.rb @@ -400,6 +400,27 @@ def present? end end + context "with #save and association having an empty array" do + before do + Her::API.setup :url => "https://api.example.com" do |builder| + builder.use Her::Middleware::FirstLevelParseJSON + builder.use Faraday::Request::UrlEncoded + builder.adapter :test do |stub| + stub.get("/users/10") { |env| [200, {}, { :id => 10, comments: [] }.to_json] } + stub.put("/users/10") { |env| [200, {}] } + end + end + + Foo::User.use_api Her::API.default_api + end + + it "successfuly saves the record" do + @user = Foo::User.find(10) + @user.save + @user.comments.should == [] + end + end + context "with #new" do it "creates nested models from hash attibutes" do user = Foo::User.new(:name => "vic", :comments => [{:text => "hello"}])