Ruby Tips #327 – to_sql and to_a for ActiveRecord Relations

ActiveRecord relation objects have a couple of methods that make our job a bit easier. Rails to_sql and Rails to_a allow to see the query without running the action and to_a allows for a throwback to the older days when ActiveRecord returned arrays not relation objects. >> User.where(:beta_user =>...

Ruby Tips #326 – Get mp4 Information from Rails Console

There are two requirements to get to retrieving information from Rails console. 1) $ brew install ffmpeg 2) In your Gemfile add `gem ‘streamio-ffmpeg’` 3) Now load up a Rails console and get some data irb(main):001:0> movie = FFMPEG::Movie.new("~/Movies/clips/evilminion_ilSJ9olk.mp4") => # movie.duration => 46.09 🙂

Ruby Tips #325 – Another Javascript Tip ‘originalEvents’

When working with mobile browsers in Rails it is important note jQuery doesn’t always give you the full Javascript API. jQuery will strip some event properties to conserve memory. In this case let’s look at the ‘touchmove’ event. To get all the properties, in pure jQuery, of a ‘touchmove’...

Ruby Tips #324 – Use Object.new in Forms

In Rails form_for the documentation indicates to pass an instance of on object to the erb template for the form. As we don’t want to litter our controller with creating new objects we can create the object in the template instead. Both these examples act exactly the same. The...

Ruby Tips #323 – Array #sample() Pulls Random Values

Ruby’s array class has a method #sample() good for randomizing array output values. [user_1, user_2, user_3, user_4].sample #=> user_3 [user_1, user_2, user_3, user_4].sample #=> user_1 It also takes a parameter of an integer. This works especially well when grabbing random data collections for sidebars or suggested reading. # expensive...

Ruby Tips #322 – Create User Slugs in Rails

Here is a user class example with a method to create a URL ready slug. class User < ActiveRecord::Base def display_name user_display_name || "#{first_name} #{last_name}" end def create_slug self.slug = self.display_name.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '') end end 🙂

Ruby Tips #321 – Use a Postgres Function in ActiveRecord

Postgres has many functions available for use. In this example we will look at how to utilize one of these functions in ActiveRecord. This formula will return the age not the date of post. The method age() is now added to the Post model. posts = Post.select('*, age(created_at)) posts.each...

Ruby Tips #320 – Execute a Writable Common Table Expression

Yesterday was an introduction into common table expressions in ActiveRecord. Let’s look at how to strip data from a large table segmenting our DB into more manageable chunks. This example could be run with a script every week or every month depending on how large your dataset grows. It...

Ruby Tips #319 – Store SQL Strings for General Use

In today’s example let’s observe how to store an an SQL string for use in other methods. Looking at a ‘Common Table Expression’ query to reuse in other places in my application. class Courses < ActiveRecord::Base def self.cumulative_grades(instance) where("#{table_name}.id IN (#{cte_sql_for(instance)})") end def self.cte_sql_for(instance) tree_sql =