Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Jade Steffen
Jade Steffen
1,842 Points

Why does Rails limit 1 when calling Post.find? Shouldn't this query only ever return 1 record?

The SQL output has a limit statement that does not seem to be needed.

2 Answers

Shaneil Clarke
Shaneil Clarke
4,630 Points

Say when finding an id, this can either be a specific id (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]). In the instance of a list or array of ids, I'm guessing the result will not be limited to one

Thomas Prince
Thomas Prince
23,302 Points

That is the expectation when you use find with one argument. The Limit clause is probably there to ensure that the expectation can be relied on regardless of the state of the database. Sometimes it's ok to be extra explicit even though it might not be necessary.

I imagine it was a conscious choice also to be consistent in the implementation of any method that the client expects only one item returned.