This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example to destroy all users matching the given condition | |
User.find_by(email: "[email protected]").destroy | |
User.where(email: "[email protected]", rating: 4).destroy_all | |
# Example to delete all users matching the given condition | |
User.find_by(email: "[email protected]").delete | |
User.where(email: "[email protected]", rating: 4).delete_all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rails 6 | |
> User.first.update_columns(office_email: '[email protected]') | |
SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]] | |
Traceback (most recent call last): | |
1: from (irb):1 | |
ActiveModel::MissingAttributeError (can't write unknown attribute `office_email`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rails 5.2 | |
> User.first.update_columns(office_email: '[email protected]') | |
SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]] | |
UPDATE "users" SET "office_email" = $1 WHERE "users"."id" = $2 [["office_email", "[email protected]"], ["id", 1]] | |
=> Traceback (most recent call last): | |
1: from (irb):8 | |
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column "office_email" of relation "users" does not exist) | |
LINE 1: UPDATE "users" SET "office_email" = $1 WHERE "users"."id" = $2 | |
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example to destroy all users matching the given condition using destroy_by | |
User.destroy_by(email: "[email protected]") | |
User.destroy_by(email: "[email protected]", rating: 4) | |
# Example to destroy all users matching the given condition using delete_by | |
User.delete_by(email: "[email protected]") | |
User.delete_by(email: "[email protected]", rating: 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rails 5.2 | |
> User.count | |
SELECT COUNT(\*) FROM "users" | |
=> 3 | |
> User.all.touch_all | |
=> Traceback (most recent call last):1: from (irb):2 | |
NoMethodError (undefined method 'touch_all' for #<User::ActiveRecord_Relation:0x00007fe6261f9c58>) | |
> User.all.each(&:touch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rails 6 | |
> User.count | |
SELECT COUNT(*) FROM "users" | |
=> 3 | |
> User.all.touch_all | |
UPDATE "users" SET "updated_at" = ? [["updated_at", "2019-04-20 16:10:40.490507"]] | |
=> 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
User.all.touch_all(time: Time.new(2019, 4, 12, 1, 0, 0)) | |
User.all.touch_all(:created_at) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rails 6 | |
class User < ApplicationRecord | |
validates :name, presence: true | |
end | |
> user = User.new | |
=> #<User id: nil, name: nil, email: nil, rating: 0, password: nil, created_at: nil, updated_at: nil> | |
> user.valid? | |
=> false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rails 5.2 | |
class User < ApplicationRecord | |
validates :email, presence: true | |
end | |
> user = User.new | |
=> #<User id: nil, name: nil, email: nil, rating: 0, password: nil, created_at: nil, updated_at: nil> | |
> user.valid? | |
=> false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rails 6 | |
> user = User.new | |
=> #<User id: nil, name: nil, email: nil, rating: 0, password: nil, created_at: nil, updated_at: nil> | |
> user.valid? | |
=> false | |
> user.errors | |
=> #<ActiveModel::Errors:0x00007fc46700df10 @base=##<User id: nil, name: nil, email: nil, rating: 0, password: nil, created_at: nil, updated_at: nil>, @messages={:email=>["can't be blank"], :password=>["can't be blank"]}, @details={:email=>[{:error=>:blank}], :password=>[{:error=>:blank}]}> |
OlderNewer