cerebris / jsonapi-resources

A resource-focused Rails library for developing JSON:API compliant servers.
http://jsonapi-resources.com
MIT License
2.32k stars 532 forks source link

Relationships on Create for non ActiveRecord models #929

Open diegoeche opened 7 years ago

diegoeche commented 7 years ago

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.

lgebhardt commented 7 years ago

@diegoeche The relationships are updated in separate methods from replace_field, such as replace_to_many_links. Let me know if you are still having trouble and I'll see if I can make some time to test with Redis.

diegoeche commented 7 years ago

Thanks for your reply!

So I tried using replace_field but field_data had has_one empty. Also I'm using 0.8.1

lgebhardt commented 7 years ago

Ok, I'll try setup a simple app using Redis. Might be a day or two.