Ruby on Rails
Wednesday, October 10, 2018
Thank you very much, Rob ! I will add a unique index to Users#username column (I had one but without unique option).What about wrapping the call into transaction ? Will it be correct to call retry ? If I got it right, retry will take another call toUser.find_or_create_by(username: some_value)but this time will fetch the existing record ? Or not ?
find_or_create by is really simple
def find_or_create_by(attributes, &block) find_by(attributes) || create(attributes, &block) end
I don't think you need any transaction here. If you hit retry - that's because there is a conflicting record.
if you retry find_or_create_by - then the find_by part will succeed (because there is a conflicting record.)
Hobbyist Software is a trading name of Hobbyist Software Limited. Registered office 12 Fraley Rd, Bristol, BS93BS. Registered in England. Company no:7876492
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACTOy%2BrAwW%3DwbUyvAJJDjsp%2BPPM2qCZJ-KuGVBCyoj6Rq-Ufew%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment