Why I still prefer Thin Models in Rails

“Fat models, skinny controllers.” That was the mantra for years. But if you’ve ever worked on a Rails app that’s more than two years old, you know where that leads: a User.rb file that’s 2,000 lines long and handles everything from authentication to generating PDF invoices. The Problem with Fat Models When a model knows too much, it becomes impossible to test in isolation. You want to test a simple validation, but you end up triggering five callbacks that send emails and ping Slack hooks. ...

February 11, 2026 · 2 min · Chen Kinnrot

Building your own rails form builder

If you’re building forms with rails, whether you’re using a gem for it or working with pure rails forms, you should know this. Rails uses form builder to allow you to call all the standard label/input/select methods, the exact class name is ActionView::Helpers::FormBuilder When calling ‘form_for’ you get a fresh instance of this FormBuilder which allows you to define your form elements. Lets say you wanna add another reusable form element, for example a form section with title that uses your own custom style/classes. ...

August 7, 2019 · 2 min · Chen Kinnrot

From jQuery to Stimulus

I tried to build an SPA without a shiny client side framework, I wanted to build something fast with good user experience and keeping it as simple as possible. I decided to take rails, use turbolinks and a avoid javascript till its a must. It didn’t take more than a few hours and I found myself writing javascript. What I needed to do is simple, I had an input with number, and 2 buttons next to it, one to increase values by 1 and on to decrease it looked like this: ...

May 22, 2018 · 4 min · Chen Kinnrot

Ruby async await

There is a lot of buzz about asyc await from the javascript world, the concept is very simple and make your code much more readable. You want to execute something without blocking the main thread but you want the next line of code to run once the non blocking code finish, meaning continue code execution in its written order. Ruby has a great concurrency gem which basically encapsulate low level threading and synchronization code to common patterns like Future, Promise, Actor and much more. ...

August 8, 2017 · 3 min · Chen Kinnrot

Octopress 101

I decided to develop my own blog like all the other cool developers. If you got here, this is what I got so far, it’s not too much, but it’s a start. When I develop something my rules are very simple Avoid writing any code. Keep it simple. Easy to deploy on free hosting environment. Decent code syntax highlight So I heard(mostly in www) people talking about jekyll as a static web site generator and began to dig dipper, I searched a few ruby gems for blogging and found a gem called [octopress](https://github.com/octopress/octopress octopress v3). They, the guys who developed it, calls it Jekyll’s Ferrari, sounds good to me, looked pretty straight forward so I gave it a spin. ...

July 25, 2017 · 3 min · Chen Kinnrot

Watch out for reference duplication instead of instance duplication

# This code will generate 96 instancesViewStatData = Struct.new(:total, :target, :ratio)96.times.map {|_|ViewStatData.new(0, 0, 0)}# And this will not, it’ll generate 96 pointers[ViewStatData.new(0, 0, 0)] * 96# So Watch out!!!

July 18, 2017 · 1 min · Chen Kinnrot

Ruby Lazy chunked hash like behavior

When we want to iterate a long list, we can simply write a query and get a cursor, ActiveRecord will do all the heavy lifting for us. What happens when we need to do some complicated computations on a set of data, which sometimes can be too big to be stored in memory for the entire computing process? This is when we need to start being more creational. I’d like to introduce what I came up with. The problem: - Complex calculation on time based data series for a period of 3 months. - Each calculation may depends on previous one and on future and past data. - Must be in order. - When fetching all data server crash on memory. The solution: I wanted to do the most minor code change possible, and currently the data was accessed via a hash. I decided to encapsulate the hash with something I called lazy chunked hash (tried google it see it as standard behavior in clojure). It looks like this: class ValuesProvider def initialize() @loaded_date = nil @hash = Hash.new(0) end def [](time_slot) get(time_slot) end private def get(time_slot) relevant_date = time_slot.to_date unless relevant_date == @loaded_date load(relevant_date) end @hash[time_slot.to_i] endend Pretty simple and does the work, instead of loading the data all at once, the data is being loaded for each day separately, this way we keep it chunky but not too chunky. And best part, my code that consume the data, didn’t change because of the [] method, which makes my ValueProvider behave like an array. This solution is good when the consumer data request(call for[]) implies on what data should be loaded, which most of the times will, but in some cases it won’t)

June 5, 2017 · 2 min · Chen Kinnrot

Rubymine on OSX memory issues

I found this only today, you should run Rubymine as 32 bit process. It works much faster and consume half the memory. To set this got to the Applications folder right click on rubymine->get info and check the “open in 32 bit mode” That’s all!

January 6, 2015 · 1 min · Chen Kinnrot

Get the user locale from http headers

If you want to provide default localization support for guest user in your website yo can locate their browser locale by the following code: def extract_locale_from_header unless request.env[‘HTTP_ACCEPT_LANGUAGE’].nil? return request.env[‘HTTP_ACCEPT_LANGUAGE’].scan(/^[a-z]{2}/).first end ‘en’ end This way you can determine on server side the locale of the user currently entered your site.

November 15, 2014 · 1 min · Chen Kinnrot

Dedicating a DJ(Delayed job) worker to a specific queue in heroku.

Let’s say you have a vey important procedure that takes a while, and you need to process it in background, but you still want to execute ASAP. I’m using Delayed Job on Heroku, and could not find a lot of tutorials to do this simple task. So here is the simplest way to achieve this ability, you can raise a process/daemon that will have only one queue to work on. (Dealyed job worker by default is queue agnostic, just process all jobs) In procfile: urgentworker: QUEUE=urgent bundle exec rake jobs:work You can call the worker in any name you want and even define multiple workers for multiple queues. And in the “urgent” job just define the queue name to be “urgent” the worker will process only jobs in this queue. example: handle_asynchronously :some_job, :queue => “urgent’ This will also work for Resque. Notice that for rescue you can write QUEUE=* but for delayed job you can’t.

November 15, 2014 · 1 min · Chen Kinnrot