The Towpath (1864) - Johan Barthold Jongkind

Using has_many :through for nested 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. 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 behaviour on nested has_many relations. Lets imagine a scenario where we have a nested has_many structure as follows: Country (has_many :regions) -> Region (has_many :cities) -> City (has_many :districts) -> District Models and tables of the structure: ...

August 20, 2016 · 3 min · Serhat M. Dündar
Environs de Grenoble (1873) - Johan Barthold Jongkind

Boolean type words in YAML

YAML is a widely used data serialization language. In almost any software project, or during any dev-ops tasks, you can come across with YAML. For example Ruby on Rails uses YAML for fixtures, configuration files and localization. CI/CD tools such as CircleCI and Travis also use YAML for configuration. If you ever experienced a strange behaviour with YAML, you may have used one of the many reserved words of YAML. YAML reserves some words such as ‘yes’, ’no’, ‘y’, ’n’, ‘off’, ‘on’, etc. for boolean type. For example: ...

June 7, 2016 · 1 min · Serhat M. Dündar
Halage En Hollande (1867) - Johan Barthold Jongkind

Calculating similarity between two data with Ruby and Elasticsearch

I recently had to find similar data located in a dataset, in order to find potential duplicate records: "John Doe 123456789" "John Foe 123123123" After considering a couple of options, I’ve decided to continue with Elasticsearch, as it was already integrated in the project I was working on. The Ruby client of Elasticsearch provided a useful function on search results, records.each_with_hit, that I could abuse for this situation: file = File.open("some_file_path", "w") User.all.each do |u| r = User.search "*#{u.full_name}*" file.write("Searching for: #{u.id_number}-#{u.full_name}\n") r.records.each_with_hit {|r, hit|}.map{|k, v| "#{k.id_number}-#{k.full_name}: #{v._score}"}.each do |y| unless (u.id_number == y.split("-")[0]) if y.split(": ")[1].to_f > 1.2 file.write(" #{y}\n") end end end file.write("***********************************\n\n") end The 1.2 value in the script can be adjusted according to your needs. It will basically represent how close two values are. You might choose to increase it, if you want your results to cover a wider range of records, or decrease it if you are only interested in values looking very similar. Here is a sample output for the script: ...

December 8, 2015 · 1 min · Serhat M. Dündar