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 =

Ruby Tips #318 – Use Postgres to Concatenate Values

ActiveRecord #where statements provide a direct connection to the datastore. Today we’ll look at an example of adding values at the DB level. This is a bit faster than using Ruby, although harder to test. def post_rank(value) return where(nil) if value.nil? rank_score = "#{ created_at.to_i } + total_votes +...

Ruby Tips #317 – Select First Model Where Not In Group

In ActiveRecord we can get the first row of a table that is not part of some related criteria. In this example we are going to retrieve the next course where a student has yet to receive a grade. If we were to omit the .first(), we could get...

Ruby Tips #316 – Sticky CSS Footer with Boostrap Sass/Rails

Bootstrap is part of many web applications in the Ruby world. It is possible to create a sticky footer with navbar-fixed-bottom. The following will give you a navigation the sticks to the bottom of your browser with all the benefits of being nested in the class=”container”. :delete %> 🙂

Ruby Tips #315 – Convert Integers to Time in Ruby

Often we need to convert an integer to a time object. This can be done with the Time#at method. obj = from_outside_service obj['datetime'] #=> 1389559678 boat = Boat.create({ remote_created_at: Time.at(obj['datetime']) }) boat.remote_created_at #=> 2014-01-12 12:47:58 -0800