Skip to content

Conversation

jassa
Copy link
Contributor

@jassa jassa commented Apr 26, 2013

Hey there,
I found the following weird behavior:

Let's say a Post belongs_to a User, and there is a post without a user_id

post = Post.first # post without user_id
post.user.try(:name)
ActiveResource::ResourceNotFound: Failed.  Response code = 404.  Response message = Not Found .

On my logs I see it tried to get users/.xml

18:49:59 web.1 | Started GET "/api/users/.xml" for 127.0.0.1 at 2013-04-25 18:49:59 -0500
18:49:59 web.1 | ActionController::RoutingError (No route matches [GET] "/api/users/.xml"):

This PR fixes this, and returns nil instead:

post.user.try(:name)
=> nil

What do you think?

Additionally you might want to raise an ArgumentError when doing Post.find nil (same thing happens)

@tamird
Copy link
Contributor

tamird commented May 24, 2013

👍

@sikachu
Copy link
Member

sikachu commented Jun 26, 2013

Would you mind adding a test for this?

@jassa
Copy link
Contributor Author

jassa commented Jun 26, 2013

Ok, I added a couple of tests

guilleiguaran added a commit that referenced this pull request Jul 15, 2013
Ensure belongs_to finder method sends a non-nil key
@guilleiguaran guilleiguaran merged commit bf8536f into rails:master Jul 15, 2013
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

Successfully merging this pull request may close these issues.

4 participants