Class: ActiveRecord::Relation
- Inherits:
-
Object
- Object
- ActiveRecord::Relation
- Defined in:
- lib/active_record/mass_assignment_security/relation.rb
Instance Method Summary collapse
- #create(attributes = nil, options = {}, &block) ⇒ Object
- #create!(attributes = nil, options = {}, &block) ⇒ Object
- #find_or_create_by(attributes, options = {}, &block) ⇒ Object
- #find_or_create_by!(attributes, options = {}, &block) ⇒ Object
- #find_or_initialize_by(attributes, options = {}, &block) ⇒ Object
-
#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. -
#first_or_create!(attributes = nil, options = {}, &block) ⇒ Object
Like
first_or_create
but callscreate!
so an exception is raised if the created record is invalid. -
#first_or_initialize(attributes = nil, options = {}, &block) ⇒ Object
Like
first_or_create
but callsnew
instead ofcreate
. - #new(attributes = nil, options = {}, &block) ⇒ Object
Instance Method Details
#create(attributes = nil, options = {}, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/active_record/mass_assignment_security/relation.rb', line 19 def create(attributes = nil, = {}, &block) if attributes.is_a?(Array) attributes.collect { |attr| create(attr, , &block) } else attrs = respond_to?(:values_for_create) ? values_for_create(attributes) : attributes scoping { klass.create(attrs, , &block) } end end |
#create!(attributes = nil, options = {}, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/active_record/mass_assignment_security/relation.rb', line 29 def create!(attributes = nil, = {}, &block) if attributes.is_a?(Array) attributes.collect { |attr| create!(attr, , &block) } else attrs = respond_to?(:values_for_create) ? values_for_create(attributes) : attributes scoping { klass.create!(attrs, , &block) } end end |
#find_or_create_by(attributes, options = {}, &block) ⇒ Object
85 86 87 |
# File 'lib/active_record/mass_assignment_security/relation.rb', line 85 def find_or_create_by(attributes, = {}, &block) find_by(attributes.respond_to?(:to_unsafe_h) ? attributes.to_unsafe_h : attributes) || create(attributes, , &block) end |
#find_or_create_by!(attributes, options = {}, &block) ⇒ Object
89 90 91 |
# File 'lib/active_record/mass_assignment_security/relation.rb', line 89 def find_or_create_by!(attributes, = {}, &block) find_by(attributes.respond_to?(:to_unsafe_h) ? attributes.to_unsafe_h : attributes) || create!(attributes, , &block) end |
#find_or_initialize_by(attributes, options = {}, &block) ⇒ Object
81 82 83 |
# File 'lib/active_record/mass_assignment_security/relation.rb', line 81 def find_or_initialize_by(attributes, = {}, &block) find_by(attributes.respond_to?(:to_unsafe_h) ? attributes.to_unsafe_h : attributes) || new(attributes, , &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'>
63 64 65 |
# File 'lib/active_record/mass_assignment_security/relation.rb', line 63 def first_or_create(attributes = nil, = {}, &block) first || create(attributes, , &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!
.
70 71 72 |
# File 'lib/active_record/mass_assignment_security/relation.rb', line 70 def first_or_create!(attributes = nil, = {}, &block) first || create!(attributes, , &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
.
77 78 79 |
# File 'lib/active_record/mass_assignment_security/relation.rb', line 77 def first_or_initialize(attributes = nil, = {}, &block) first || new(attributes, , &block) end |
#new(attributes = nil, options = {}, &block) ⇒ Object
13 14 15 16 17 |
# File 'lib/active_record/mass_assignment_security/relation.rb', line 13 def new(attributes = nil, = {}, &block) attrs = respond_to?(:values_for_create) ? values_for_create(attributes) : attributes scoping { klass.new(attrs, , &block) } end |