-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serializer pulls relations when belongs_to declared but not included #1552
Comments
for ember, I know it uses those ids to get the associations... but I've been reading that it's better to use @rails-api/ams Do you think disabling auto-getting ids for the relationships hash should be toggleable via some option? |
Providing the ids is good, because right, ember-data uses that, but in case of 'belongs_to' association it doesn't require pulling whole related objects. We have ids right on a parent model and probably their class (to generate type attribute) can be taken from reflection, I guess. |
Well, what was mentioned in #1555 is that if you have a ton of related records via has-many, it would throw that ton of ids into your model without a choice. I think the broader issue here is that ids are always pulled in to the resulting json, but aren't always desired. |
Well, yeah... maybe fields param could be useful here? Or any other mean to control what will be included in resulting json. As for pulling ids when they are needed, it's not a problem, as active_record does pretty good job with |
I think the bug here is same as #1555 (comment) that relationships in jsonapi should not be called when not included |
Ok, will I can confirm that this is the behavior that is currently happening, cause I see it in my ember app. |
They're not being rendered, right? Just queried? On Wed, Mar 9, 2016 at 8:43 AM L. Preston Sego III notifications@github.com
|
not the whole model, no. just the relationship list of ids and types per relationship. |
Which is correct, I assume. The only problem that it makes unnecessary queries to db. |
yeah, @youroff at least for belongs_to -- it shoudl be capable of getting the id without querying. has_many can get pretty crazy though |
just for summary for a future PR
yeah? I think that could be two separate PRs |
Not sure how it would fit current API, but would be really awesome to do something like:
|
@youroff I gave a thought about 3) a while ago, and the main issue is that we somehow need to consistently infer the |
@beauby Yeah, I played a bit and it seemed to be accessible through AR reflections. For non-ar I agree, they probably should be treated differently. |
Related:
Related code: in JSONAPI::Resources, the relationship object has a
see https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/relationship.rb for full code module JSONAPI
class Relationship
attr_reader :foreign_key
def initialize(name, options = {})
@foreign_key = options[:foreign_key] ? options[:foreign_key].to_sym : nil
end
def belongs_to?
false
end
class ToOne < Relationship
attr_reader :foreign_key_on
def initialize(name, options = {})
super
@foreign_key ||= "#{name}_id".to_sym
@foreign_key_on = options.fetch(:foreign_key_on, :self)
end
def belongs_to?
foreign_key_on == :self
end
class ToMany < Relationship
def initialize(name, options = {})
super
@foreign_key ||= "#{name.to_s.singularize}_ids".to_sym
end
end
end
end
|
Anyone else hitting this issue again in the latest release? We had no problems in 0.10.6 but are hitting this exact issue in 0.10.9. Edit: Tested with 0.10.7-0.10.9 all experience this issue while 0.10.6 is fine |
@ajuni7 please open a new issue. I appreciate the work you've done so far to isolate the version. I'll help you fix it (discuss there) if you're up for it. |
@ajuni7 can you please confirm if setting the config option introduced in v0.10.7 would solve the case?
|
Expected behavior vs actual behavior
As long as belongs_to relationship is not declared in
include
parameter, it shouldn't be loaded during serialization. Currently it's loaded.Steps to reproduce
And in controller:
Environment
ActiveModelSerializers 0.10.0.rc4
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
Rails 4.2.4
The text was updated successfully, but these errors were encountered: