आप lib/mongoid/finders.rb में स्रोत पर नज़र डालें तो:
# Find the first +Document+ given the conditions, or creates a
# with the conditions that were supplied.
...
# @param [ Hash ] attrs The attributes to check.
#
# @return [ Document ] A matching or newly created document.
def find_or_create_by(attrs = {}, &block)
find_or(:create, attrs, &block)
end
आप देख सकते हैं कि find_or_create_by एक {}
पहले तर्क के रूप में स्वीकार करता है। आप
something.find_or_create_by(name: 'john', age: 20)
पर कई स्थितियों में बस पास कर सकते हैं और इसे काम करना चाहिए।
स्रोत
2012-08-16 11:19:05
बहुत बहुत धन्यवाद! – hyperrjas
मैं केवल पहली विशेषता से कैसे पा सकता हूं, और फिर - केवल कुछ भी नहीं मिला है - अन्य विशेषताओं के साथ बनाएँ? – ChristofferJoergensen
@CristofferJoergensen, Client.create_with (लॉक: झूठा) .find_or_create_by (first_name: 'एंडी'), दस्तावेज़ों पर एक नज़र डालें: http://guides.rubyonrails.org/active_record_querying.html – mkralla11