Skip to content

Add tests for Issue #1467: ActiveModel resources support#20

Merged
takaokouji merged 2 commits into
masterfrom
fix/rails7-activemodel-resources-1467
Jan 21, 2026
Merged

Add tests for Issue #1467: ActiveModel resources support#20
takaokouji merged 2 commits into
masterfrom
fix/rails7-activemodel-resources-1467

Conversation

@takaokouji
Copy link
Copy Markdown
Collaborator

Summary

Adds comprehensive tests for Issue JSONAPI-Resources#1467 and documents the correct approach for using ActiveModel-based models with jsonapi-resources.

Problem

Issue JSONAPI-Resources#1467 reported that ActiveModel-based models fail with undefined method 'where' errors in v0.10.7+. Users were confused about why their non-ActiveRecord models stopped working.

Root Cause

The default JSONAPI::Resource inherits from ActiveRelationResource, which expects ActiveRecord query methods like:

  • where, order, limit, offset
  • Other ActiveRecord::Relation methods

ActiveModel::Model doesn't provide these methods, causing failures.

Solution

Use JSONAPI::BasicResource for non-ActiveRecord models:

class SimpleModelResource < JSONAPI::BasicResource
  model_name 'SimpleModel'
  
  attribute :name
  attribute :description
  
  # Override methods for custom data access
  def self.find_by_key(key, options = {})
    # Custom implementation
  end
  
  def self.find_fragments(filters, options = {})
    # Custom implementation
  end
end

Tests Added

  1. active_model_resource_test.rb - Demonstrates BasicResource working correctly with ActiveModel

    • find_fragments with and without filters
    • find_by_key
    • No ActiveRecord dependencies
  2. active_model_broken_test.rb - Shows the problem with ActiveRelationResource

    • Using default JSONAPI::Resource fails with NoMethodError
    • Confirms the error is about missing ActiveRecord methods

Testing

  • All 695 tests pass
  • New tests verify both working and broken scenarios
  • Serves as documentation for future users

Documentation

This PR serves as living documentation showing:

  • ✅ How to correctly use BasicResource with ActiveModel
  • ❌ Why using Resource (ActiveRelationResource) fails
  • 📝 Clear examples for future users

Related

🤖 Generated with Claude Code

…port

Issue JSONAPI-Resources#1467 reported that ActiveModel-based models fail with
'undefined method where' errors in v0.10.7+.

Root cause:
- The default JSONAPI::Resource inherits from ActiveRelationResource
- ActiveRelationResource expects ActiveRecord query methods (where, order, etc.)
- ActiveModel::Model doesn't provide these methods

Solution:
- ActiveModel-based models should use JSONAPI::BasicResource
- BasicResource doesn't assume ActiveRecord and works with any model

Tests added:
1. active_model_resource_test.rb - Shows BasicResource works with ActiveModel
2. active_model_broken_test.rb - Demonstrates the problem with ActiveRelationResource

The tests confirm that using BasicResource is the correct approach for
non-ActiveRecord models.

Related: https://github.com/cerebris/jsonapi-resources/issues/1467
ActiveModel::Attributes is only available in Rails 5.2+. For Rails 5.1
compatibility, use attr_accessor instead.

Changes:
- Remove 'include ActiveModel::Attributes'
- Replace 'attribute :id, :integer' with 'attr_accessor :id'
- Add explicit initialize method

This ensures the test fixtures work with Rails 5.1.7 and above.
@takaokouji takaokouji merged commit 953ec76 into master Jan 21, 2026
60 checks passed
@takaokouji takaokouji deleted the fix/rails7-activemodel-resources-1467 branch January 21, 2026 12:27
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.

2 participants