SSH into Root
$ ssh [email protected]
Change Root Password
SSH into Root
$ ssh [email protected]
Change Root Password
As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.
All the complexity is handled by ActiveRecord::Transactions
. Any model class or instance has a method named .transaction
. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.
Замечали ли вы, что когда вы набираете что-то в google или yandex с ошибками, вас поправляют?
Возможно вы искали ... ?
Данная концепция называется триграммным поиском, она позволяет искать слова и фразы с опечатками.
Каждое слово делится на сочетания из 3х букв – триграммы. На примере слова "Россия", будет:
class Example < ActiveRecord::Base | |
## included modules & attr_* | |
## associations | |
## plugins | |
## named_scopes | |
## validations | |
## callbacks | |
## class methods | |
## public methods | |
## protected methods |
module CheckedAttributes | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
def attr_checked(attribute, &validation) | |
define_method "#{attribute}=" do |value| | |
raise 'Invalid attribute' unless validation.call(value) | |
instance_variable_set("@#{attribute}" , value) | |
end |
def screen_shot_and_save_page | |
require 'capybara/util/save_and_open_page' | |
path = "/#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}" | |
Capybara.save_page body, "#{path}.html" | |
page.driver.render Rails.root.join "#{Capybara.save_and_open_page_path}" "#{path}.png" | |
end | |
begin | |
After do |scenario| | |
screen_shot_and_save_page if scenario.failed? |
# lib/custom_logger.rb | |
class CustomLogger < Logger | |
def format_message(severity, timestamp, progname, msg) | |
"#{timestamp.to_formatted_s(:db)} #{severity} #{msg}\n" | |
end | |
end | |
logfile = File.open("#{Rails.root}/log/custom.log", 'a') # create log file | |
logfile.sync = true # automatically flushes data to file | |
CUSTOM_LOGGER = CustomLogger.new(logfile) # constant accessible anywhere |