Skip to content
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

Bug: Associations that have an attribute or method named map cannot be serialized #471

Open
benlieb opened this issue Aug 1, 2020 · 1 comment

Comments

@benlieb
Copy link

benlieb commented Aug 1, 2020

The following method in relationship.rb uses a check for respond_to? :map to determine whether an association is plural or not:

    def ids_hash_from_record_and_relationship(record, params = {})
      return ids_hash(
        fetch_id(record, params)
      ) unless polymorphic

      return unless associated_object = fetch_associated_object(record, params)

      return associated_object.map do |object|
        id_hash_from_record object, polymorphic
      end if associated_object.respond_to? :map

      id_hash_from_record associated_object, polymorphic
    end

However I have a model that has a field/attribute called map. When this model is an association it doesn't get serialized properly.

The association gets put in included fine, but in the relationships key I get the value of the map attribute:

image

And this cannot be properly parsed, and the relationship is lost. This was a particularly difficult issue to debug, mostly because all the values of my map property while I was trying to debug this were nil. So it wasn't clear it was a bug at first. I spent a couple of hours trying to figure out what was wrong with MY code. It was only after reading your source code that I was able to work out the problem. I'm surprised no one else has had this issue.

Do you have a recommended workaround for this?

Also I suspect that it is not your intention to require models to not have attributes or methods named map. Ideally this could be changed rather quickly in your source code. Otherwise, I'll have to rename the attribute in a rather large application, made harder by the fact that the native iterator map is also used extensively, so a big mess really.

@davidwparker
Copy link

FYI #462

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

No branches or pull requests

2 participants