first_or_create
dường như ít tài liệu hơn, vì vậy tôi tự hỏi liệu đó có phải chỉ vì hai phương thức là đồng nghĩa.first_or_create vs find_or_create_by
6
A
Trả lời
14
Về cơ bản:
Foo.where(attributes).first_or_create
là giống như:
Foo.find_or_create_by(attributes)
#first_or_create
đôi khi bị hiểu lầm như mọi người mong đợi nó để tìm kiếm theo các thuộc tính nhất định, nhưng đó không phải là trường hợp. Aka
Foo.first_or_create(attributes)
Sẽ không tìm kiếm foo
thỏa mãn attributes
. Nó sẽ lấy foo
đầu tiên nếu có. Sẽ hữu ích nếu điều kiện tìm kiếm là tập hợp con của những người được sử dụng để tạo. Aka
Foo.where(something: value).first_or_create(attributes)
Sẽ tìm đầu tiên foo
nơi something: value
. Nếu không có mặt, nó sẽ sử dụng attributes
để tạo ra nó.
Các vấn đề liên quan
- 1. Rails 3 find_or_create_by
- 2. Rails first_or_create Phương thức ActiveRecord
- 3. Điều kiện chủng tộc trong Rails first_or_create
- 4. Lỗi khi sử dụng `find_or_create_by` trên liên kết` has_many` `qua`
- 5. First_or_create có cập nhật về trận đấu không?
- 6. J2ME VS Android VS iPhone VS Symbian VS Windows CE
- 7. Exec vs ExecWait vs ExecShell vs nsExec :: Exec vs nsExec :: ExecToLog vs nsExec :: ExecToStack vs ExecDos vs ExeCmd
- 8. bigtable vs cassandra vs simpledb vs dynamo vs couchdb vs hypertable vs riak vs hbase, họ có điểm gì chung?
- 9. ACE vs Boost vs Poco vs wxWidgets
- 10. NetSqlAzMan vs AzMan vs (?????)
- 11. & vs * và | vs +
- 12. * vs ** vs *** trong Proguard?
- 13. Mathematica: Unevaluated vs Defer vs Hold vs HoldForm vs HoldAllComplete vs etc etc
- 14. ember.js + tay lái: render vs outlet vs vs vs vs control
- 15. innerText vs innerHtml vs nhãn vs văn bản vs textContent vs outerText
- 16. Htmlentities vs addslashes vs mysqli_real_escape_string
- 17. Delegate.CreateDelegate vs DynamicMethod vs Biểu
- 18. RazorEngine vs RazorTemplates vs RazorMachine
- 19. HttpRequest vs HttpRequestMessage vs HttpRequestBase
- 20. getchar_unlocked() VS scanf() VS cin
- 21. gruntjs vs yeoman vs guard
- 22. pycuda vs theano vs pylearn2
- 23. Coroutine vs Continuation vs Generator
- 24. DataView.RowFilter Vs DataTable.Select() vs DataTable.Rows.Find()
- 25. Shmem vs tmpfs vs mmap
- 26. Processing.js vs. CAKE vs. Raphael.js
- 27. Mongoose.model vs Connection.model vs Model.model
- 28. MySQL vs Firebird vs SQLite
- 29. window.onload vs. body.onload vs. document.onready
- 30. Selenium vs. SimpleTest vs. WatiN