Jsonapi-resources: has_one foreign_key_on: :related fails on save

Created on 6 Apr 2016  路  4Comments  路  Source: cerebris/jsonapi-resources

When saving a resource with a has_one relationship with foreign_key_on: :related I get an error of undefined method 'foreign_key=' for the model. I'm not sure if the intent is to change the relationship on the related items when save or not. For now I did the following patch to not update any relationships that have foreign_key_on: :related

def replace_to_one_link(relationship_type, relationship_key_value)
  if(self.class._relationships[relationship_type.to_sym]).belongs_to?
    super
  end
end

def replace_polymorphic_to_one_link(relationship_type, relationship_key_value, relationship_key_type)
  if(self.class._relationships[relationship_type.to_sym]).belongs_to?
    super
  end
end
Bug

Most helpful comment

This seems to be related to #391. I am having the same issue but with all of my has_one relationships.

Adding this line below referenced from issue #391 removes the errors for me.

def model_id=(_value)
   # TODO: Remove once it's fixed
end

All 4 comments

This seems to be related to #391. I am having the same issue but with all of my has_one relationships.

Adding this line below referenced from issue #391 removes the errors for me.

def model_id=(_value)
   # TODO: Remove once it's fixed
end

Yeah...this one jumps up in my Ember-Data+JSONAPI-Resources world every so often.

One pattern I've noticed is that my Resource relationships defined with foreign_key_on: :related should simply be excluded from the payload sent from my client.

With Ember-Data, I tell my model's serializer to not serialize that :related relationship. So to borrow from @Baril27 model example above, I'd edit my Ember-Data serializer as follows:

// ember-project/app/serializers/model-with-has-one-fk-on-related.js
import ApplicationSerializer from './application';

export default ApplicationSerializer.extend({
  attrs: {
    /* ----------------------------------------------------- HAS-ONE-ON-RELATED RELATIONSHIPS */
    model: { serialize: false }
  }
});

I ran into this issue recently. My Ember Data model defines a belongsTo. My JR Resource defines a foreign_key_on: :related. Here's what I did:

  def _replace_to_one_link relationship_type, relationship_key_value, options
    relationship = self.class._relationships[relationship_type]
    return :completed unless relationship.belongs_to?

    super
  end

Seems like JR should pay attention to foreign_key_on: :related. My use cases/requirements, currently, don't deal with polymorphic resources. Additionally, the reason I elected to override the lower-level method is to keep access to the callbacks. YMMV.

Hi everyone. I'm having this issue right now on version "0.9.11". All of the suggestions here point to a direction where the relationships are not created (or, to be more specific, not added to the through table to associate both models). Did anyone find a solution to save it on the same post request ? Cheers from Brazil!

Was this page helpful?
0 / 5 - 0 ratings