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

Change behaviour of find #195

Merged
merged 2 commits into from
Sep 26, 2017
Merged

Change behaviour of find #195

merged 2 commits into from
Sep 26, 2017

Conversation

andrykonchin
Copy link
Member

It's common to get exception if one or more records can not be found for the requested ids

Both Mongoid and ActiveRecord support this behaviour (here and here)

Current behaviour - find returns nil or smaller array.
New behaviour - it raises RecordNotFound if one or more records can not be found for the requested ids

@coveralls
Copy link

coveralls commented Sep 25, 2017

Coverage Status

Coverage increased (+0.003%) to 96.874% when pulling 3328346 on andrykonchin:fix-finder-behaviour into 193bd52 on Dynamoid:master.

Copy link
Collaborator

@richardhsu richardhsu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding more consistency! Makes easier to move from ActiveRecord to Dynamoid in this case!

Just one small suggestion for error messaging similar to ActiveRecord.

expects_array ? Array(result) : result
else
find_all(ids)
result = find_all(ids)
raise Errors::RecordNotFound if result.size != ids.size
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to provide the error message too?

ActiveRecord will say which id was passed and for an array of ids it'll say which ids passed and it'll produce "found X results, but was looking for N" kind of message so we could help report the difference.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coveralls
Copy link

coveralls commented Sep 26, 2017

Coverage Status

Coverage decreased (-0.02%) to 96.855% when pulling a7dc3b3 on andrykonchin:fix-finder-behaviour into 193bd52 on Dynamoid:master.

Copy link
Member

@pboling pboling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve unless we can add a spec for the find => find_by_id change.

@@ -107,7 +107,7 @@ def build(attrs = {})
def exists?(id_or_conditions = {})
case id_or_conditions
when Hash then where(id_or_conditions).first.present?
else !! find(id_or_conditions)
else !! find_by_id(id_or_conditions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would like to see a spec for any behavior change we expect from switching to find_by_id. This looks like it might be a significant change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pboling find uses find_by_id internally. The only difference between them - find supports passing multiple ids. As far as I see Document.exists? accepts either one id or condition.

So Document.exists?([1, 2, 3]) will not work. Do you think we should keep this behaviour?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pboling Looks like neither ActiveRecord nor Mongoid supports exists?(array) signature (here and here) and user can check multiple ids in the following way
Document.exists?('id.in': [1, 2, 3])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think the change is fine then. Seems we did not have a spec for the old behavior of passing an array directly then.

@pboling pboling merged commit f21d988 into Dynamoid:master Sep 26, 2017
@andrykonchin andrykonchin deleted the fix-finder-behaviour branch September 26, 2017 19:09
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