Method: Her::Model::Relation#first_or_initialize

Defined in:
lib/castle-her/model/relation.rb

#first_or_initialize(attributes = {}) ⇒ Object

Fetch a resource and build it if it’s not found

Examples:

@user = User.where(:email => "[email protected]").find_or_initialize

# Returns the first item of the collection if present:
# GET "/[email protected]"

# If collection is empty:
@user.email # => "[email protected]"
@user.new? # => true


154
155
156
# File 'lib/castle-her/model/relation.rb', line 154

def first_or_initialize(attributes = {})
  fetch.first || build(attributes)
end