Monthly Archives: February 2014

No gravatar for me.

Thought about posting a nice sketch of me as an avatar for this blog, but after seeing that they condone hardcore sexual imagery or extreme violence then that’s not something I can support.

I’ll have to check the link between WordPress and Gravatar and decide on the future of the blog accordingly.

What’s the Rails way?

Right, I finally figured out the way to get tabular data sorted on the index page by way of a non-numeric associated table B, by way of a class method and scope (to limit the selection to the first letter of the name in table A) but it means that my controller needs to check the value the column selection param and run a different query when sorting by that column.

I could add a custom route to the controller and just render the index page. But that will probably mean splitting my session variable processing across different actions.

I guess what I really should do is put all the session processing in the before_action setup method and keep the index and custom actions neat and tidy.

And then there’s the question of the matter of the simple select form to display only items in table A that have a reference to table B as pointed to by the select.

Or perhaps I consider all this as basic search functionality and incorporate into the search actions that I will need to do anyway.

One step at at a time: tidy up the session handling.

When I say mud, I mean mud.

Finally, a break from the rain and a bit of sun. Out in the garden, red kites swirling overhead, planting the bulbs donated by father-in-law, digging in the garden manure and last year’s leaf mould and hoping that all the effort will help the soil.

An to finish, a couple of hours pulling out the twigs from the pile of leaves and bagging up for next year’s compost.

I’ll leave the glamour part of the gardening to my wife while I do the only stuff i can be safely entrusted to: the mud and rotting leaves.

I’m not writing a blog application or social network!

Why does every tutorial or rails example post assume only blogs are written? Does anyone actually use Rails for real stuff?

I’m writing a telephone extension allocation system to be used by FIM to disj out phone numbers to Lync; posts and comments have no relevance.

I say this because I need to work out how to do column sorting on association tables using left joins and I really can’t get my head around the finer points of ActiveRecord’s includes and joins to make any progress.

I nearly had it cracked with a (mostly) manual query but it broke will_paginate, having an array rather than AR relations. So near, yet so far.

I’m determined to crack this one, so I might go quiet for a little while.

Session handling with pagination

Having managed to improved the alphabetic pagination in my applications is due to better handling of the session tokens in the controller, but while going through Railscast 228 (more on tabular sorting to follow – using many:many associations if I can figure it out) Ryan mentioned the need for some basic security around the submitted data and I realised that I was assigning session vars directly from params.

But it’s easy enough to tidy up.

session.delete :letter if params[:q]
session[:letter] ||= ('a'..'z').include?(params[:q]) ? params[:q] : ''

Remove the session value if a query string param has been passed. Then if the session value doesn’t exist assign it from the params only if the patam value is in the range a-z; otherwise assign and empty string.

This works out well for me as the empty string is the cue to display all values by virtue of it being the value passed to the search scope.

But going back to the Railscast, I was really taken with this construct,

%w[asc desc].include?(params[:direction]) ?  params[:direction] : "asc"

for input sanitsation and realising while some of my recent stuff has been okay, I’ve still got a fair way to go.

Now, back to this column sorting…

back breaking beckons

Now there’s been the appearance of some fine weather today, the potential that it might last a bit longer means I’ll be out in the garden next weekend with the fork and making sure last year’s leaf mould compost is properly dug in to the veg patch and ready for the new season.

Also very chuffed about the appearance of this year’s rhubarb.

Ruby on Rails alphabetic pagination

1. Create a model scope to limit the searching

# Filter host names by their first letter - used for alphabetic pagination
scope :by_letter, ->(initial) {where("name LIKE \'#{initial}%\'").order(:name) }

2. Make the search criteria available to the controller and keep it sticky in the session

    session.delete :object_letter if params[:q]
    session[:object_letter] ||= params[:q]

3. Limit the records selected by the index

@objects = Object.by_letter(session[:object_letter]).paginate :page => params[:page], :per_page => 25

4. Prepare a list of intials and ensure that they are set in the controller only when indexing and searching

before_action :set_initials, only: [:index, :search]
  def set_initials
    @first_letters ="DISTINCT LOWER(SUBSTR(, 1, 1)) AS name").order("name").collect{|fl| "#{}"}

5. Construct a partial template to create the form for display


<% @first_letters.each do |lt|%>
  <%= lt != (session[:object_letter]) ? link_to(lt, objects_url+"?q="+lt )+" " : "<span class='digg_pagination disabled'>#{lt}</span>".html_safe %>
<% end %>
<%= link_to('All', objects_url+"?q=" ) %>

This is not a shared partial template because it includes specific URL references.

6. Render the partial on the index page underneath the main numerical pagination.

<%= render :partial => 'by_letter' %>