Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

If I use two different attributes to load a record, it gets materialized as two different records, instead of one #571

Closed
darthdeus opened this Issue · 7 comments

7 participants

@darthdeus
Collaborator

Given a server implementation that looks like this

def show
  respond_with User.find_by_username_or_id(params[:id])
end

If I do App.store.find(1) which makes a request GET /users/1 and returns

{
  id: 1,
  username: "johndoe"
}

and then I do App.store.find("johndoe") which makes a request GET /users/johndoe and returns the same JSON, the result is that now I have two different users stored in the identity map.

This is because Ember Data will store the initial ID used to query, even if it is different from the one returned by the server.

I'm not sure if this is intentional (probably not), because Ember already knows which attribute is the primary key. It should either use that in the identity map, or raise an exception that the record which was returned has a different primary key than the one which was used to query, because of this exact scenario.

This becomes an issue when I want to transition to a route which shows that user, it will look like /users/johndoe, and if I try to query the user's ID attribute, it returns the username.

user = App.User.find("johndoe") // returns { id: 1, username: "johndoe" }
user.get("id") // => "johndoe"
@raila

user = App.User.find("johndoe") --> find() thinks "johndoe" is the id!
calling App.User.find(id) always interpretes the provided string as an id.
You would have to call App.User.find({username: "johndoe"}) which uses findQuery internally.

At least that is how I understand the inner workings of the find() method.

@darthdeus
Collaborator

@raila you can't do that, because findQuery returns an array of results, not a single record. See #551 for more detail

@kevinansfield

Did you ever find a solution to this? I'm running into a lot of issues with duplicate records in the identity map and having to work around that all over the place because of this.

@Globegitter

Is there any advancement/update on this?

@wagenet
Owner

#551 addresses this better.

@wagenet wagenet closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.