Skip to content

it bails out when asked for an empty nested has_many relationship (undefined method `[]' for nil:NilClass) #366

@x-yuri

Description

@x-yuri

When you just created an issue, the attachments relationship is unusable (client.Issue.build.save.attachments gives "undefined method [] for nil:NilClass"). That happens because attachments are nested under fields. And in such case the parent must exist.

Right now I have to do:

attachments = issue.attrs.has_key?('fields') \
  ? issue.attachments \
  : jira.Issue.find(issue.id).attachments
attachments.build.save!(file: p)

Alternatively:

if issue.attrs.has_key?('fields')
  issue.attachments.build.save!(file: p)
else
  JIRA::Resource::Attachment.new(client, issue: issue).save!(file: p)
end

I first tried client.Attachment.build(issue: issue).save!(file: p), but it doesn't work since build()'s arguments are passed down in the attrs key. E.g. initialize() looks in options[:issue], but the relation is in options[:attrs][:issue].

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementEnhancements to existing features

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions