Profile cover photo
Profile photo
Marcello Raffaele Avagliano

Post has attachment
Handy hint for better database performance in Ruby on Rails:

Always add DB index in your Migration!
By default, rails does not add indexes automatically for foreign key, you should add indexes by yourself.
If you want to speed up your performance, I suggest to add an index for

– foreign key
– columns that need to be sorted
– lookup fields
– and columns that are used in a GROUP BY

read more ...

Post has attachment
Generate polymorphic URL in Rails

In this example, I want to explain a best practice for Polymorphic URLs in Rails. I will show really bad things here as an illustrative example.
Don’t worry, we will refactor the code later ;-)

Let us assume that we have three models, Post, News and Comment. In most cases, a post has many comments and a news has many comments. 

They are defined like:

class Post < ActiveRecord::Base
  has_many :comments
class News < ActiveRecord::Base
  has_many :comments
class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true


Post has attachment
Let’s go on with part four of our mathematical soultions-series in Project Euler:

The Largest palindrome product

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers.

We first need a palindrome method. Let’s define it like this:
def palindrome? x
  # convert into String
  y = x.to_s
  # define look as the length of "y" divided by 2
  look = y.length / 2
  y[0..(look-1)] == y[-look..-1].reverse

Read on ...

Post has attachment
Ever asked yourself how to use HTTP Basic authentication with ActiveAdmin?
There are several reasons to use HTTP Basic authentication for your backend. I needed a solution which can be recycled wherever I need a HTTP Basic authentication. I only wanted a central place for User and Password and I wanted just one call wherever I need my authentication.
Lets go …

# app/controllers/application_controller.rb

  def authenticate
    authenticate_or_request_with_http_basic do |username, password|
      username == "admin" && password == "superpassword"

Now you can call
before_filter :authenticate

… whenever you need a HTTP Basic authentication.
In case of ActiveAdmin, let’s call our before_filter like that:

# config/initializers/active_admin.rb
ActiveAdmin.setup do |config|

  # http auth for admin area
  config.before_filter :authenticate


Have fun!

Post has attachment
forever friends, thats cool :-)

Post has attachment
Why the Rails default_scope is a bad thing

Activerecord provides default_scope to set a default scope for all operations on the model. I was never a friend of using default_scope, even when many friends used it in their projects. At the first look, it looks convenient but will lead to a headache very soon. We should avoid using it and here is why:

Assume we defined a default_scope in Comment model
class Comment
  default_scope where(published: true).order("created_at desc")

In this case the default_scope added some behaviors that we don’t recognize at the first look. The bad news: You can’t override default_scope by default! It lists comments order by created_at. Let’s have a look

> Comment.limit(10)
  Comment Load (3.3ms)  SELECT `comments`.* FROM `comments` WHERE `comments`.`published` = 1 ORDER BY created_at desc LIMIT 10

Read the full article in my blog:

Post has attachment
When ever we create a project, the same issue is our pain in the ass …
I talk about Security which is somehow dull and seems to be boring. I have done a simple checklist for a quick review of your code, which is divided in three simple steps: model, view and of course controller.

Our quick security checklist for your models
Use the helper method

if you have to explicitly identify attributes that are accessible by the actions “create” and “update_attributes”. Don’t ever think that someone won’t try to post a value to your form.
Another thing to be aware is, when we use

instead of

in this case we have the the advantage that it fails if new fields are added to a model. In this case you have to expose new fields explicitly. The method attr_accessible specifies a white list of model attributes that can be set via mass-assignment.

Read the full article "Ruby on Rails Security Checklist" here

Post has attachment
As I showed in my article (see bottom line), we can have another way of doing this job:
----- <code> -----
require 'prime'
goal = 600851475143

Prime.each(goal) do |number|
   @container= [number]
puts @container.last
----- </code> -----

This small piece of code illustrates togeter with my other solutions, that we have a vast amount of ways doing things in Ruby …

Post has attachment
now you can see, how much fun we it-professionals have ;-)

The Great Office War

This goes into category "coffee break" ...
My favourite excuse is the new word of the day:

My code did not fail!
I just found another method that will not solve this problem ...

Wait while more posts are being loaded