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

Initialize an association/collection if it doesn't already exist? #313

Open
mwpastore opened this issue Aug 23, 2016 · 2 comments
Open

Initialize an association/collection if it doesn't already exist? #313

mwpastore opened this issue Aug 23, 2016 · 2 comments

Comments

@mwpastore
Copy link

I'm retrieving a user with sideloaded identities, adding (additional) identities, and saving the user. If the user already has some identities, it works fine. However, if they don't, and I try to add some, I get the following error:

/Users/mwp/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/zendesk_api-1.14.0/lib/zendesk_api/actions.rb:32:in `save!': undefined method `connection' for nil:NilClass (NoMethodError)
    from /Users/mwp/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/zendesk_api-1.14.0/lib/zendesk_api/actions.rb:47:in `save'
    from /Users/mwp/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/zendesk_api-1.14.0/lib/zendesk_api/collection.rb:366:in `block in _save'
    from /Users/mwp/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/zendesk_api-1.14.0/lib/zendesk_api/collection.rb:364:in `map!'
    from /Users/mwp/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/zendesk_api-1.14.0/lib/zendesk_api/collection.rb:364:in `_save'
    from /Users/mwp/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/zendesk_api-1.14.0/lib/zendesk_api/collection.rb:143:in `save'
    from /Users/mwp/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/zendesk_api-1.14.0/lib/zendesk_api/actions.rb:74:in `block in save_associations'
    from /Users/mwp/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/zendesk_api-1.14.0/lib/zendesk_api/actions.rb:66:in `each'
    from /Users/mwp/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/zendesk_api-1.14.0/lib/zendesk_api/actions.rb:66:in `save_associations'
    from /Users/mwp/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/zendesk_api-1.14.0/lib/zendesk_api/actions.rb:30:in `save!'

I suspect I need to initialize the identities collection on the user if it doesn't already exist. Here's what I'm trying, but it's not working (no change in behavior, still get the NoMethodError). Any suggestions would be appreciated!

user.identities ||= ZendeskAPI::Collection.new(zendesk, ZendeskAPI::User::Identity)
# later...
user.identities << { :type => 'email', :value => '[email protected]' }
# later...
user.save!

Thank you in advance.

@mwpastore
Copy link
Author

Something is up here but my understanding of the issue is flawed. Closing this until I can provide a more accurate test case.

@mwpastore
Copy link
Author

mwpastore commented Aug 24, 2016

Okay, let's try this again. The problem is that if you side-load an associated collection, you can't add something to that collection and save it (or its parent record). If you don't side-load (or add anything), it works fine. For example:

user = zendesk.users.find!(:id => 9728708027)
user.identities.build!(:type => 'email', :value => '[email protected]')
user.identities.save! # => true
user.save! # => true
user = zendesk.users.find!(:id => 9728708027, :include => :identities)
user.identities.build!(:type => 'email', :value => '[email protected]')
user.identities.save! # => NoMethodError
user.save! # => NoMethodError
user = zendesk.users.find!(:id => 9728708027, :include => :identities)
user.identities.save! # => true
user.save! # => true

Hopefully that helps track down the issue. I'll stop side-loading for the time being.

@mwpastore mwpastore reopened this Aug 24, 2016
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

1 participant