diff --git a/lib/her/model/parse.rb b/lib/her/model/parse.rb index db43d98f..93ebfccb 100644 --- a/lib/her/model/parse.rb +++ b/lib/her/model/parse.rb @@ -58,8 +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? - if association[:class_name].constantize.include_root_in_json? + if her_nearby_class(association[:class_name]).include_root_in_json? root = association[:class_name].constantize.root_element hash[association[:data_key]] = params.map { |n| n[root] } else diff --git a/spec/model/parse_spec.rb b/spec/model/parse_spec.rb index 4ef1bc32..9d03c748 100644 --- a/spec/model/parse_spec.rb +++ b/spec/model/parse_spec.rb @@ -342,4 +342,44 @@ def to_params expect(user.to_params).to eql(:user => {:first_name => 'Someone'}) end end + + context 'objects or attributes empty for an association' do + before do + Her::API.setup :url => "https://api.example.com", :send_only_modified_attributes => true do |builder| + builder.use Her::Middleware::FirstLevelParseJSON + builder.use Faraday::Request::UrlEncoded + end + + spawn_model "User" do + has_many :pets + end + spawn_model "Pet" + + end + + subject { User.build(fullname: "Tobias Fünke", pets: []) } + + it 'should not raise an error while trying to parse to params' do + expect{ subject.to_params }.to_not raise_error + end + end + + context 'parsing an association of a class in a module' + before do + Her::API.setup :url => "https://api.example.com", :send_only_modified_attributes => true do |builder| + builder.use Her::Middleware::FirstLevelParseJSON + builder.use Faraday::Request::UrlEncoded + end + + spawn_model "Foo::User" do + has_many :pets + end + spawn_model "Foo::Pet" + end + + subject { Foo::User.build(fullname: "Tobias Fünke", pets: []) } + + it 'should not raise an error while trying to parse to params' do + expect{ subject.to_params }.to_not raise_error + end end