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

Use take instead of first in ActiveRecord::Relation#first_or_create #25173

Closed
wants to merge 1 commit into from

Conversation

guigs
Copy link
Contributor

@guigs guigs commented May 28, 2016

This change was suggested in discussion of #24847.

first adds an order by clause to query that may make it slower.

…ate`, `#first_or_create!` and `#first_or_initialize`

`first` adds an order by clause to query that may make it slower.
@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @eileencodes (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@guigs
Copy link
Contributor Author

guigs commented May 28, 2016

r? @rafaelfranca

@matthewd
Copy link
Member

@rafaelfranca @sgrif given they're private, shouldn't we rename them to match the new implementation?

If we want to backport to 5-0-stable as a bug fix, I agree we should keep the existing name there.

@sgrif
Copy link
Contributor

sgrif commented May 28, 2016

They're private, but we know they have pretty wide usage. I think renaming them would be too much unnecessary breakage.

@guigs
Copy link
Contributor Author

guigs commented May 28, 2016

How about add take_or_create and deprecate first_or_create in 5.0 and remove first_or_create in 5.1?

@sgrif
Copy link
Contributor

sgrif commented May 28, 2016

I don't think we should be removing first_or_create

On Sat, May 28, 2016 at 9:42 AM Guilherme Goettems Schneider <
notifications@github.com> wrote:

How about add take_or_create and deprecate first_or_create in 5.0 and
remove first_or_create in 5.1?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#25173 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/ABdWKxU21sTu-eW5GcjkRSvG5buqCMGQks5qGEY3gaJpZM4Io89h
.

@jeremywadsack
Copy link
Contributor

Any further thoughts on this change? Just ran into it and discovered the ORDER BY defeated our index on the fields in the where clause preceding first_or_create in the chain.

@matthewd
Copy link
Member

matthewd commented Jul 8, 2017

@sgrif I'm not sure which change you were suggesting above.

If we're not prepared to rename first_or_create etc, even though they're not documented, because we think people are using them anyway.. it surely follows that we oughtn't change their behaviour either?

@jeremywadsack
Copy link
Contributor

Just to note, these methods were documented in Rails 3.2. So many code bases still depend on them (not to mention developers).

What about adding new take_or_create methods? Then developers can move to the new one?

@rails-bot
Copy link

rails-bot bot commented Dec 18, 2019

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Dec 18, 2019
@rails-bot rails-bot bot closed this Dec 25, 2019
@simi
Copy link
Contributor

simi commented Dec 25, 2019

@rafaelfranca @matthewd I still think this is relevant. I think going with new methods take_or_* will make sense. There's PR for that opened - #24847.

It makes sense for me to keep first_or_* being ordered and take_or_* not. Same works for first and take. Making first_or_ methods being not ordered will make this inconsistent in my eyes.

Order is usually unnecessary from my experience on first_or_ calls. But I can imagine some apps rely on that. Ordering can be really expensive in database and it is IMHO best practice to avoid it when not necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants