Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method `map' for nil:NilClass for association #523

Closed
noctivityinc opened this issue Feb 18, 2019 · 6 comments
Closed

undefined method `map' for nil:NilClass for association #523

noctivityinc opened this issue Feb 18, 2019 · 6 comments

Comments

@noctivityinc
Copy link

Here is my model:

class UserDeviceOffer < Base
  include Her::Model
  include_root_in_json true
  parse_root_in_json true, format: :active_model_serializers
  collection_path "users/:user_id/user_device_offers"
  resource_path "user_device_offers/:id"
  request_new_object_on_build true

  belongs_to :user
  # has_one :device, :class_name => "Device", :foreign_key => "user_device_offer_id"

end

and when I try to interact with it in any way I get the above error, which points to here:

  # @private
        def instantiate_collection(klass, parsed_data = {})
          records = klass.extract_array(parsed_data).map do |record|
            instantiate_record(klass, data: record)  ## FAILS ON THIS LINE
          end
          Her::Collection.new(records, parsed_data[:metadata], parsed_data[:errors])
@zacharywelch
Copy link
Collaborator

Do you have a sample response and the call which is producing the error?

@noctivityinc
Copy link
Author

Here is the full chain:

2.6.1 :002 > u = User.find('XXXX-XXXX')
ETHON: performed EASY effective_url=http://domain.com/v1/admin/users/XXXX-XXXX response_code=200 return_code=ok total_time=2.526228
D, [2019-02-18T08:43:05.160459 #67471] DEBUG -- : [httplog] Sending: GET
D, [2019-02-18T08:43:05.160681 #67471] DEBUG -- : [httplog] Data:
D, [2019-02-18T08:43:05.160856 #67471] DEBUG -- : [httplog] Status: 200
D, [2019-02-18T08:43:05.161028 #67471] DEBUG -- : [httplog] Benchmark: 2.526608 seconds
D, [2019-02-18T08:43:05.161482 #67471] DEBUG -- : [httplog] Response: (not showing binary data)
 => #<User(users/XXXX-XXXX) active=true full_name="Marcelo Drakeford " first_name="Marcelo" email="adfadf@yahoo.com" stripe_id="cus_Ch7PG7epplUbph" created_at="2018-04-17T02:03:51.260Z" updated_at="2018-10-20T22:55:55.303Z" utc_offset=-5 alpha_firmware_access=false beta_firmware_access=false expert_mode=false enable_remote_logging=false devices_count=1 user_device_offers_count=1 activities_count=0 stripe=nil payment={"exists"=>false, "current"=>false, "needed"=>true, "expired"=>true} id="1ecec758-5fb7-456c-b10f-70b2512821cc">

2.6.1 :003 > u.user_device_offers.all
 => #<Her::Model::Associations::HasManyAssociation:0x00007ff42ee7d1d0 @parent=#<User(users/1ecec758-5fb7-456c-b10f-70b2512821cc) active=true full_name="Marcelo Drakeford " first_name="Marcelo" email="asdfadsf@yahoo.com" stripe_id="cus_Ch7PG7epplUbph" created_at="2018-04-17T02:03:51.260Z" updated_at="2018-10-20T22:55:55.303Z" utc_offset=-5 alpha_firmware_access=false beta_firmware_access=false expert_mode=false enable_remote_logging=false devices_count=1 user_device_offers_count=1 activities_count=0 stripe=nil payment={"exists"=>false, "current"=>false, "needed"=>true, "expired"=>true} id="XXXX-XXXX">, @opts={:class_name=>"UserDeviceOffer", :name=>:user_device_offers, :data_key=>:user_device_offers, :default=>[], :path=>"/user_device_offers", :inverse_of=>nil}, @params={}, @klass=UserDeviceOffer, @name=:user_device_offers>

2.6.1 :004 > u.user_device_offers.first
ETHON: performed EASY effective_url=http://domain.com/v1/admin/users/XXXX-XXXX/user_device_offers response_code=200 return_code=ok total_time=0.011298
D, [2019-02-18T08:43:12.593182 #67471] DEBUG -- : [httplog] Sending: GET
D, [2019-02-18T08:43:12.593368 #67471] DEBUG -- : [httplog] Data:
D, [2019-02-18T08:43:12.593503 #67471] DEBUG -- : [httplog] Status: 200
D, [2019-02-18T08:43:12.593624 #67471] DEBUG -- : [httplog] Benchmark: 0.011605 seconds
D, [2019-02-18T08:43:12.593912 #67471] DEBUG -- : [httplog] Response: (not showing binary data)
Traceback (most recent call last):
        1: from (irb):4
NoMethodError (undefined method `map' for nil:NilClass)

@noctivityinc
Copy link
Author

@zacharywelch was that what you were looking for? Here's even more logs:

oMethodError: undefined method `map' for nil:NilClass
  File "/home/vcap/deps/0/vendor_bundle/ruby/2.6.0/gems/her-1.1.0/lib/her/model/attributes.rb", line 182, in instantiate_collection
  File "/home/vcap/deps/0/vendor_bundle/ruby/2.6.0/gems/her-1.1.0/lib/her/model/attributes.rb", line 193, in new_collection
  File "/home/vcap/deps/0/vendor_bundle/ruby/2.6.0/gems/her-1.1.0/lib/her/model/http.rb", line 72, in block in get
  File "/home/vcap/deps/0/vendor_bundle/ruby/2.6.0/gems/her-1.1.0/lib/her/model/http.rb", line 59, in request
  File "/home/vcap/deps/0/vendor_bundle/ruby/2.6.0/gems/her-1.1.0/lib/her/model/http.rb", line 81, in get_raw
  File "/home/vcap/deps/0/vendor_bundle/ruby/2.6.0/gems/her-1.1.0/lib/her/model/http.rb", line 70, in get
  File "/home/vcap/deps/0/vendor_bundle/ruby/2.6.0/gems/her-1.1.0/lib/her/model/associations/association.rb", line 52, in fetch
  File "/home/vcap/deps/0/vendor_bundle/ruby/2.6.0/gems/her-1.1.0/lib/her/model/associations/has_many_association.rb", line 86, in fetch
  File "/home/vcap/deps/0/vendor_bundle/ruby/2.6.0/gems/her-1.1.0/lib/her/model/associations/association_proxy.rb", line 11, in tap

@zacharywelch
Copy link
Collaborator

That should be enough @noctivityinc. Not sure why a separate request is made to pick up the first device offer since they should be cached after .all. Tied up at the moment but I'll try and reproduce later.

@noctivityinc
Copy link
Author

noctivityinc commented Feb 20, 2019 via email

@noctivityinc
Copy link
Author

@zacharywelch figured it out. Since the model is UserDeviceOffers the serialized json root is userdeviceoffersso I needed to add:

parse_root_in_json :userdeviceoffers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants