Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/mass_assignment_security/relation.rb

Instance Method Summary collapse

Instance Method Details

#find_or_create_by(attributes, options = {}, &block) ⇒ Object



56
57
58
# File 'lib/active_record/mass_assignment_security/relation.rb', line 56

def find_or_create_by(attributes, options = {}, &block)
  find_by(attributes) || create(attributes, options, &block)
end

#find_or_create_by!(attributes, options = {}, &block) ⇒ Object



60
61
62
# File 'lib/active_record/mass_assignment_security/relation.rb', line 60

def find_or_create_by!(attributes, options = {}, &block)
  find_by(attributes) || create!(attributes, options, &block)
end

#find_or_initialize_by(attributes, options = {}, &block) ⇒ Object



52
53
54
# File 'lib/active_record/mass_assignment_security/relation.rb', line 52

def find_or_initialize_by(attributes, options = {}, &block)
  find_by(attributes) || new(attributes, options, &block)
end

#first_or_create(attributes = nil, options = {}, &block) ⇒ Object

Tries to load the first record; if it fails, then create is called with the same arguments as this method.

Expects arguments in the same format as Base.create.

Examples

# Find the first user named Penélope or create a new one.
User.where(:first_name => 'Penélope').first_or_create
# => <User id: 1, first_name: 'Penélope', last_name: nil>

# Find the first user named Penélope or create a new one.
# We already have one so the existing record will be returned.
User.where(:first_name => 'Penélope').first_or_create
# => <User id: 1, first_name: 'Penélope', last_name: nil>

# Find the first user named Scarlett or create a new one with a particular last name.
User.where(:first_name => 'Scarlett').first_or_create(:last_name => 'Johansson')
# => <User id: 2, first_name: 'Scarlett', last_name: 'Johansson'>

# Find the first user named Scarlett or create a new one with a different last name.
# We already have one so the existing record will be returned.
User.where(:first_name => 'Scarlett').first_or_create do |user|
  user.last_name = "O'Hara"
end
# => <User id: 2, first_name: 'Scarlett', last_name: 'Johansson'>


34
35
36
# File 'lib/active_record/mass_assignment_security/relation.rb', line 34

def first_or_create(attributes = nil, options = {}, &block)
  first || create(attributes, options, &block)
end

#first_or_create!(attributes = nil, options = {}, &block) ⇒ Object

Like first_or_create but calls create! so an exception is raised if the created record is invalid.

Expects arguments in the same format as Base.create!.



41
42
43
# File 'lib/active_record/mass_assignment_security/relation.rb', line 41

def first_or_create!(attributes = nil, options = {}, &block)
  first || create!(attributes, options, &block)
end

#first_or_initialize(attributes = nil, options = {}, &block) ⇒ Object

Like first_or_create but calls new instead of create.

Expects arguments in the same format as Base.new.



48
49
50
# File 'lib/active_record/mass_assignment_security/relation.rb', line 48

def first_or_initialize(attributes = nil, options = {}, &block)
  first || new(attributes, options, &block)
end