JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Relationships on Create for non ActiveRecord models

Open
#929 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

I guess more of a question.

I have this legacy app we are trying to slowly migrate to use jsonapi-resources. We have some bloated models we are splitting into smaller resources but also some models that point to Redis-backed models.

class RedisResource < JSONAPI::Resource
  attribute :attr_1
  attribute :attr_2

  # Override required. The underlying model is not based on
  # ActiveRecord.
  def self.find_by_key(key, options = {})
    context = options[:context]
    model = RedisModel.find(key)
    raise(JSONAPI::Exceptions::RecordNotFound, key) if model.nil?
    new(model, context)
  end
end

The problem though, is that somehow the relationship data is getting lost before reaching the Resource methods like: #replace_field(field_data).

All that info is available when I use an ActiveRecord model that uses the #belongs_to helper. But unsure what do I need to do/implement in my models to get the relationship assignment working.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with RedisResource#find_by_key and trace how relationship data reaches #replace_field(field_data) for non-ActiveRecord resources. Compare that path with an ActiveRecord resource using belongs_to; done when the issue’s relationship assignment works for the Redis-backed model, with the relevant behavior verified by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
redis, ruby
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.