In our last blog, we explained how to handlerolling deployments of Rails applications with no downtime. In this article we will walk you throughhow to handle graceful shutdown of processes in Kubernetes. This post assumes that you have basic understanding ofKubernetesterms likepodsanddeployments. ProblemWhen we deploy Rails applications on kubernetesit stops existing pods and spins up new ones.Whe
Rails already provides methods for creating class level and module levelvariables in the form ofcattr** and mattr** suite of methods. In Rails 5, we can go a step further and createthread specific class or module level variables. Here is an example which demonstrates an example on how to use it. module CurrentScope thread_mattr_accessor :user_permissionsendclass ApplicationController < ActionContr
Database schemas change rapidly as project progresses. And it can be difficultto track purpose of each table and each column in a large project with multipleteam members. The solution for this problem is to document data models right from Railsmigrations. Solution in Rails 4You can add comments in Rails 4.x migrations using gems likemigration_comments andpg_comment. Solution in Rails 5Rails 5 allo
In Rails 4.x we had start option in find_in_batches method. Person.find_in_batches(start: 1000, batch_size: 2000) do |group| group.each { |person| person.party_all_night! }endThe above code provides batches of Person starting from record whose value ofprimary key is equal to 1000. There is no end value for primary key. That means in the above case all therecords that have primary key value greater
Rails 5 introduces Array Inquirerthat wraps an array object and provides friendlier methods to check for thepresence of elements that can be either a string or a symbol. pets = ActiveSupport::ArrayInquirer.new([:cat, :dog, 'rabbit'])> pets.cat?#=> true> pets.rabbit?#=> true> pets.elephant?#=> falseArray Inquirer also has any? method to check for the presence of any of thepassed arguments as elemen
Before Rails 5, we could fetch all time zones for US by using us_zones methodas follows. > puts ActiveSupport::TimeZone.us_zones.map(&:to_s)(GMT-10:00) Hawaii(GMT-09:00) Alaska(GMT-08:00) Pacific Time (US & Canada)(GMT-07:00) Arizona(GMT-07:00) Mountain Time (US & Canada)(GMT-06:00) Central Time (US & Canada)(GMT-05:00) Eastern Time (US & Canada)(GMT-05:00) Indiana (East)Such functionality of gett
Fragment cache helps in caching parts of the view instead of caching the entireview. Fragment caching is used when different parts of the view need to becached and expired separately. Before Rails 5, fragment caching was supportedonly in Action View templates. Rails 5 providesfragment caching in Action Mailer views. To use this feature, we need to configure our application as follows. config.actio
In Rails 4.x we have transactional fixtures that wrap each test in a databasetransaction. This transaction rollbacks all the changes at the end of the test.It means the state of the database, before the test is same as after the test isdone. By default this functionality is enabled. We can choose to disable it in a testcase class by setting the class attribute use_transactional_fixtures tofalse cl
Rails 5 beta-1 wasrecently released and one of the notable change was introduction ofApplicationRecord. Up to Rails 4.2, all models inherited from ActiveRecord::Base. But startingfrom Rails 5, all models will inherit from ApplicationRecord. class Post < ApplicationRecordendWhat happened to ActiveRecord::Base ? Well not much changed in reality. Following file will be automatically added tomodels in
In Rails 4.x, it is not possible to have destroy dependency on both sides of a bi-directional association between the two models as it would result in an infinite callback loop causing SystemStackError: stack level too deep. 1 2class User < ActiveRecord::Base 3 has_one :profile, dependent: :destroy 4end 5 6class Profile < ActiveRecord::Base 7 belongs_to :user, dependent: :destroy 8end 9 Copy 1 2>>
In Rails 4.x, when we save an ActiveRecord object then Rails automaticallyupdates fields updated_at or updated_on. >> user = User.new(name: 'John', email: 'john@example.com')>> user.save INSERT INTO "users" ("name", "created_at", "updated_at", "email") VALUES (?, ?, ?, ?) [["name", "John"], ["created_at", 2016-03-16 09:12:44 UTC], ["updated_at", 2016-03-16 09:12:44 UTC], ["email", "john@example.co
Let's look at a validation example in Rails 4.x. class User < ActiveRecord::Base validates :email, presence: trueend>> user = User.new>> user.valid?=> false>> user.errors.messages=> {:email=>["can't be blank"]}In this case, we do not get any information about the type of failed validationas ActiveModel#Errors only gives the attribute name and the translated errormessage. This works out well for no
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く