How to: Detect if validating inside model

In a model I needed to modify a getter to append a string if a certain attribute evaluated to true. This works great, until I needed to detect the uniqueness of the attribute against a column in the DB through the :uniqueness validator.

The problem

1
2
3
4
5
6
7
8
9
  def label
  new_value = read_attribute(:label)
 
  if self.something == true
    new_label += 'Str'
  end
 
  new_label
end

When validating against the label’s uniqueness the validator will read the label attribute from my custom getter, thus, if a record in the DB has a label of ‘Test’, and our new record has a label of ‘Test’ and self.something evaluates to true, the validator will get ‘TestStr’ from the getter and compare the custom value against the DB. Based on this setup, no match would be found. Continue reading

5 Useful PHP to Ruby methods

When I first started writing in ruby I found myself needing to know the ruby method equivalents to specific PHP functions. These methods are widely used by myself for debugging and day to day checking of data types.

PHP’s die(‘message’) becomes

1
abort 'message'

PHP’s get_class_methods() becomes

1
object.methods.inspect

PHP’s in_array() becomes

1
array.include? 'item'

Continue reading

Where this is going and YAPS discontinued

The last few months have proved to be a tough time for myself. During the past few months I have finalised a divorce and clean break order, started a new position at an up and coming cloud service and moved into a new home. In turn, I haven’t had the chance to concentrate on YAPS.

While I thoroughly enjoy writing plugins and particularly the YAPS framework my heart currently lies with Ruby on Rails development, being that my job relies on it, and that I find it astoundingly enjoyable to code in.

Having a vast amount of ideas for small web apps that could be useful to many, further progressing YAPS, with the time required to manage and support the plugin framework will prohibit this. Thus, I have had to make the decision to discontinue the development. Continue reading

A week with ruby on rails

For the past week I have been playing with ruby and of course, rails. My experience has been an absolute pleasure, I can’t quite express the joy I have in having the opportunity to learn, use and abuse ruby on rails.

Having come to experience ruby after 11 years with PHP and PHP alone as a server side language, the only frustration I have is learning new syntax. No, that’s not a fault of ruby’s, infact I find ruby to be an absolutely beautiful language to write in. Its so… English!
Continue reading

Life and YAPS

I haven’t posted in a little while, that’s because on the 05th I started a new role at a excellent startup, Skyscape Cloud, acting as their only web developer.

My main development skills lie with PHP, however my new role has given me an opportunity to broaden my development horizons with ruby, specifically rails.
Continue reading

When to use single quotes and double quotes

If you are familiar with PHP you will know that strings can be contained within single quotes or double quotes. More often than not developers tend to always use double quotes, despite the context of the string.

While this is OK in principle, double quotes take more processing time than that of single quotes (despite being minimal). This is because PHP expects to find variables within double quoted strings and parse them. Continue reading

7 tips to prevent PHP running out of memory

Is the following error familiar to you?

Fatal error: Allowed memory size of XXX bytes exhausted

It is to me, and recently I’ve written an import script that reads the contents of 10 XML files. These files accumulate a total of 14.9MB in size. The XML files contain page content (A good 10,000), each page has references to images and PDFs which needed to be downloaded, stored on the filesystem in the same directory structure as well as imported into an asset manager, again in the same structure. Once the assets have been downloaded the script stores the contents of the XML files. Continue reading