Rails Tips – Counter Cache (.size vs .count)

* Size (rails function) is more intelligent than count (ruby function) * Notice that when you counter cache, size makes only one call to db as opposed to count which does 2 queries. 1.9.2-p320 :057 > ParentModel.find(5765).childmodel.count RequestedTrip Load (0.3ms) SELECT `parent_models`.* FROM `parent_models` WHERE (`parent_models`.`id` = 5765) ORDER BY parent_models.created_at desc LIMIT 1 SQL […]

Continue reading


Tips and Tricks in Ruby on Rails

Use escape_javascript: When you are making an ajax call and in response sending JS that will insert the content in an HTML element, make sure you run escape_javascript on the content. For e.g.  $(“#div”).html(‘<%= variable %>’). In this case if variable’s value contains a quote ‘, the resulting JS that would be rendered in browser: $(“#div”).html(‘value_’_with_quote’). This […]

Continue reading