HELLO, I’M SERHAT AND THIS IS MY FANCY TITLE.

Rails ve PostgreSQL: Constraint'ler


Not Null Constraint ve Presence Constraint

  • foreign_key türündeki sütunlar için, eğer arada optional: true bir ilişki yoksa null: false kullanın.

  • Integer, boolean ve float türleri için null: false kullanmayın, add_null_constraint kullanın.

  • String türü için null: false kullanmayın, add_presence_constraint kullanın.

Not Null Constraint vs. Not Null Check

PostgreSQL'de NOT NULL durumu hem CONSTRAINT olarak, hem de CHECK olarak tanımlanabiliyor. CONSTRAINT ile CHECK arasında bazı farklar bulunmakta.

  • change_column_null (veya null: false) metodu sütun üzerinde CONSTRAINT tanımlaması yaparken,
  • add_presence_constraint tabloya CHECK tanımlaması yapar.
Continue reading
Date: | Tags: ruby on rails, postgresql | Categories: tech, ruby on rails, database

Rails PostgreSQL Veri Tipleri: Constraint ve Migrasyonlar


Data integrity ve referential integrity konularının epey önemli olduğu bir Rails+PostgreSQL projesinde rein GEM'ini kullanıyoruz. Constraint yönetimini oldukça kolaylaştıran bu aracı kullanırken ekip içerisinde bir takım standartlar da belirledik. İş bu doküman ekip içerisinde kullanılmak üzere yazılmış olup, benzer ihtiyaçları olanlara da faydalı olabilir düşüncesiyle paylaşılmıştır. Constraint'ler için bknz Rails ve PostgreSQL: Constraint'ler.

Boolean

  • boolean alanlar için mutlaka null_constraint eklenmelidir.

  • boolean alanlara mutlaka default bir değer tanımlanmalıdır. Örn: default: false gibi.

  • Boolean bir alanın nil olması beklenen bir durum değildir. Örneğin:

    Student.where(active: nil).count => 50
    Student.where(active: false).count => 40
    Student.where(active: true).count => 60

Bu durumda nil değerini içeren kayıtların tek tek incelenerek düzeltilmesi gerekir.

Continue reading
Date: | Tags: ruby on rails, postgresql | Categories: tech, ruby on rails, database

Encrypting Sensitive Data with Ruby on Rails


The most recent versions (5.1 and 5.2) of Ruby on Rails shipped with a feature named as encrypted credentials which replaces the secrets.yml feature and enables us to keep sensitive data in an encrypted file named as config/credentials.yml.enc. With this new feature, pushing the encrypted file to the repository wasn't an issue anymore - and in fact Rails encouraged users to push credentials.yml.enc.

However, this feature only works with a single file which is config/credentials.yml.enc. Recently we needed to add some seed-like data, which doesn't fit to credentials.yml.enc to our web application.

  • The seed data included some user information such as date of birth information, last names etc. These are not top-secret information, and they don't include critical data such as passwords etc, but it's better to keep them away from curious eyes.
  • We couldn't simply use dummy ID numbers because the application was verifying ID numbers over the government APIs, and the app can't work with fake ID numbers in general.
  • On the other hand, since we are making autonomous deployments with CircleCI and Dokku, transfering the seed file to production server after each deploy wasn't an option.
  • Of course, there are lots of professional solutions to encrypt a file and decrypt it when needed, but we didn't want to implement another complexity level or another dependency (such as GEM) to the app. Therefore we aimed to solve this problem with core capabilities of Ruby and Rails.
Continue reading
Date: | Categories: tech, ruby on rails

Using has_many :through for nested has_many relations in Rails


has_many :through is a useful association type of Rails. It's mostly popular and often used as a join model for many-to-many relations.

rails-has-many-through

However, has_many :through is more than a simple join model, because it conducts INNER JOIN(s) on related models. We can also take the advantage of this beha...

Continue reading

Running Rake Tasks in a Loop


Rake tasks in a loop, will only executed once when they are not re-enabled. Take a look to this example:


namespace :yoksis do
  desc 'fetches all references'
  task :references do
    mapping = {
      get_instruction_language: 'UnitInstructionLanguage',
      get_instruction_type: 'UnitInstructionType'
    }

    mapping.each do |action, klass|
      Rake::Task['yoksis:reference'].invoke(action, klass)
    end
  end

  desc 'fetch an individual reference'
  task :reference, %i[soap_method klass] => [:environment] do |_, args|
    puts args[:soap_method]
    puts args[:klass]
  end
end

When you run the yoksis:references task, it will only print out {get_instruction_language: 'UnitInstructionLanguage'} and will skip the second item of the mapping hash.

Continue reading
Date: | Tags: ruby, ruby on rails, rake, rake in loop | Categories: tech, ruby, ruby on rails