Skip to content

Instantly share code, notes, and snippets.

View MittalPatel-BTC's full-sized avatar

Mittal Patel MittalPatel-BTC

View GitHub Profile
@MittalPatel-BTC
MittalPatel-BTC / delete_and_destroy_all.rb
Last active May 6, 2019 10:55
Rails 5 destroy and delete feature
# 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
@MittalPatel-BTC
MittalPatel-BTC / rails_6_update_columns.rb
Created May 6, 2019 10:51
Rails 6 update_columns changes
# 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`)
@MittalPatel-BTC
MittalPatel-BTC / rails_5_update_columns.rb
Created May 6, 2019 10:52
Rails 5.2 update_columns changes
# 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
^
@MittalPatel-BTC
MittalPatel-BTC / delete_by_and_destroy_by.rb
Created May 6, 2019 10:56
Rails 6 delete_by and destroy_by methods
# 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)
# 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)
@MittalPatel-BTC
MittalPatel-BTC / touch_all.rb
Created May 6, 2019 10:59
Rails 6 new method touch_all
# 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
@MittalPatel-BTC
MittalPatel-BTC / custom_arg_touch_all.rb
Created May 6, 2019 11:01
Custom timestamp also use with touch_all method you can pass it in the arguments
User.all.touch_all(time: Time.new(2019, 4, 12, 1, 0, 0))
User.all.touch_all(:created_at)
@MittalPatel-BTC
MittalPatel-BTC / of_kind_method.rb
Created May 6, 2019 11:04
Rails 6 added new method of_kind? on ActiveModel::Errors
# 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
@MittalPatel-BTC
MittalPatel-BTC / rails_5_slice.rb
Created May 6, 2019 11:06
Rails 5.2 slice! method
# 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
@MittalPatel-BTC
MittalPatel-BTC / rails_6_slice.rb
Created May 6, 2019 11:08
Rails 6 added changes for slice! method to ActiveModel::Errors
# 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}]}>