Skip to content

Conversation

Copy link

Copilot AI commented Aug 12, 2025

The Letter::BatchesController#validate_postage_types method was failing with a NoMethodError: undefined method 'us_postage_type' for an instance of Letter::Batch when trying to validate postage types during batch updates.

Root Cause

The controller's validate_postage_types method attempts to access @batch.us_postage_type and @batch.intl_postage_type:

def validate_postage_types
  return unless @batch.letter_return_address&.us?

  if @batch.us_postage_type.present? && !%w[stamps indicia].include?(@batch.us_postage_type)
    @batch.errors.add(:us_postage_type, "must be either 'stamps' or 'indicia'")
  end
  # ...
end

However, these attributes don't exist on the Letter::Batch model - they are form parameters used for processing but not persisted to the database.

Solution

Added us_postage_type and intl_postage_type as virtual attributes using attr_accessor in the Letter::Batch model:

attr_accessor :template, :template_cycle, :us_postage_type, :intl_postage_type

This allows the form parameters to be set on the batch instance during updates and accessed during validation, without requiring database columns. The approach follows existing patterns in the codebase (e.g., from_letter attribute in ReturnAddress model).

Testing

Verified the fix handles all scenarios correctly:

  • Valid postage types ("stamps", "indicia") pass validation
  • Invalid postage types trigger appropriate validation errors
  • Empty/nil values are handled properly
  • The controller update flow works as expected

Fixes #156.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copy link
Author

Copilot AI commented Aug 12, 2025

@24c02 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

…r::Batch

Co-authored-by: 24c02 <163450896+24c02@users.noreply.github.com>
Copilot AI changed the title [WIP] [Theseus/production] NoMethodError: undefined method 'us_postage_type' for an instance of Letter::Batch Fix NoMethodError for us_postage_type on Letter::Batch instances Aug 12, 2025
Copilot AI requested a review from 24c02 August 12, 2025 18:33
Copilot finished work on behalf of 24c02 August 12, 2025 18:34
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

Successfully merging this pull request may close these issues.

[Theseus/production] NoMethodError: undefined method 'us_postage_type' for an instance of Letter::Batch

2 participants