Software Engineer working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix.
I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working at Juniper Education making code for UK schools.
Our new project Pledge Music went into Alpha just over a week ago and by all accounts its been a success.
“Pledge Music is a way for fans to help their favourite artists make records and raise money for charity. By combining new social networking technology, fan incentives and old school music biz know how, fans can visit the site to not only hear great new music, but also share the experience with friends and get actively involved in what they are listening to.”
We launched on two platforms Facebook and Pledge Music with our first project Another Last Goodbye by Marwood. The project aimed to raise $5000 in order record a CD with a percentage of the profits going to Amnesty International and met its target with 7 days of launch which we’re very pleased with.
Now we have a successful Alpha test, Marwood are entering the studio to record their album over the next few weeks and we look forward to hearing the album very soon.
Now the real work begins as we do a last push to move through Beta and into a full release over the next month with another 9 projects already in the pipeline. Go team.
I recently added counter cache to one of my assocations to try and solve a validation problem. ie. If an object has too many associated objects already then it would be invalid.
Easy, set up counter cache and update your existing database with the current totals, however this doesn’t work anymore. It doesn’t set them at all.
Subscription.all.each do |s|
s.update_attribute :requirements_count, s.requirements.length
end
I can see all the updates in my log, but minus the ‘requirements_count’ which was the whole point :S
Requirement Load (0.9ms) SELECT * FROM "requirements" WHERE ("requirements".subscription_id = 158) ORDER BY created_on asc
Subscription Update (0.9ms) UPDATE "subscriptions" SET "updated_on" = '2009-07-11 17:44:35.463992' WHERE "id" = 158
Requirement Load (1.1ms) SELECT * FROM "requirements" WHERE ("requirements".subscription_id = 264) ORDER BY created_on asc
Subscription Update (0.9ms) UPDATE "subscriptions" SET "updated_on" = '2009-07-11 17:44:35.470008' WHERE "id" = 264
I found this in the ActiveRecord changelog :( It states this is now a readonly field. Under normal circumstances I agree it should be readonly but what about when you just want to get your data into the right state?
*2.0.0 [Preview Release]* (September 29th, 2007) [Includes duplicates of changes from 1.14.2 - 1.15.3]
* Add attr_readonly to specify columns that are skipped during a normal ActiveRecord #save operation. Closes #6896 [Dan Manges]
class Comment < ActiveRecord::Base
# Automatically sets Article#comments_count as readonly.
belongs_to :article, :counter_cache => :comments_count
end
class Article < ActiveRecord::Base
attr_readonly :approved_comments_count
end
The solution it seems is to redeclare the class you are trying update the count in in the migration class. I cut and pasted this from a mailing list post I found. This seems stupidly awkward a solution and took far too long to diagnose this problem.
class AddRequirementsCountToSubscriptions < ActiveRecord::Migration
class Subscription < ActiveRecord::Base
has_many :requirements
def reset_column_information
generated_methods.each {|name| undef_method(name) }
@column_names = @columns = @columns_hash = @content_columns = @dynamic_methods_hash = @generated_methods = @inheritence_column = nil
end
end
def self.up
add_column :subscriptions, :requirements_count, :integer, :default => 0
Subscription.reset_column_information
Subscription.all.each do |s|
puts "UPDATING #{s.id}"
puts "#{s.requirements.length}"
s.update_attribute :requirements_count, s.requirements.length
end
end
def self.down
remove_column :subscriptions, :requirements_count
end
end
I found some code recently for a client that skips (or should at least) skip all filters in a controller. I haven’t seen this done before and a quick google clarified someone else has put it to use somewhere, as I wasn’t sure if this should ever have worked.
class FooController < ApplicationController
skip_filter filter_chain
end
It was actually implemented with a little more functionality to set options for all the filters that are being skipped. e.g only for particular actions in a controller.
class ApplicationController
def self.skip_all_filters(options = {})
skip_filter filter_chain, options
end
end
However, this doesn’t appear to work in post Rails 2.1 applications. So I found myself using the following instead.
class ApplicationController
def self.skip_all_filters(options = {})
filter_chain.map(&:method).each do |filter|
skip_filter filter, options
end
end
end
class FooController < ApplicationController
before_filter :do_something
skip_all_filters :only => [:blah, :blah2]
end
I just updated the RSpec gem today since seeing from The RSpec book update that there is a new release.
rl@bloodandguts:~/project$ ./script/spec_server
Loading Rails environment
*****************************************************************
DEPRECATION WARNING: you are using deprecated behaviour that will
be removed from a future version of RSpec.
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
* spec_server is deprecated.
* please use spork (gem install spork) instead.
*****************************************************************
So…here we go.
rl@bloodandguts:~/project$ sudo gem install spork
Successfully installed spork-0.5.7
1 gem installed
Installing ri documentation for spork-0.5.7...
Updating ri class cache with 7915 classes...
Installing RDoc documentation for spork-0.5.7...
Update the spec helper with the bootstrap command
rl@bloodandguts:~/project$ spork --bootstrap
Using RSpec
Bootstrapping /home/rl/project/spec/spec_helper.rb.
Done. Edit /home/rl/project/spec/spec_helper.rb now with your favorite text editor and follow the instructions.
The spec_helper will have instructions on how to edit the file although I mostly took the previous contents of the file and placed it in the ‘Spork.prefork’ block.
require 'rubygems'
require 'spork'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
end
Spork.each_run do
# This code will be run each time you run your specs.
end
# --- Instructions ---
# - Sort through your spec_helper file. Place as much environment loading
# code that you don't normally modify during development in the
# Spork.prefork block.
# - Place the rest under Spork.each_run block
# - Any code that is left outside of the blocks will be ran during preforking
# and during each_run!
# - These instructions should self-destruct in 10 seconds. If they don't,
# feel free to delete them.
#
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
Starting the server with just ‘spork’
rl@bloodandguts:~/project$ spork
Using RSpec
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
Normally I do all of my image manipulation on Windows with Photoshop as I am not a big fan of GIMP, so I’ve never had to install a new font before. Not immediately obvious but you can do it like so.
I’ve been trying to develop a small Facebook application and FBJS is getting in the way somewhat. We have an existing application which we can easily plug in to Facebook with a few tweaks but sadly most if not all of our jQuery is completely useless.
Facebook add security re-jig all of your javascript into its own namespace by prefixing ‘applicationid_’ to each function and variable and then only allow limited access to javascript by means of implementing their own slightly modified version FBJS.
Unfortunately this means our jQuery flounders and we have to rewrite to get the same functionality. One potential saving grace is this little script that someone has come up with
Its a re-write of basic jQuery functionality in FBJS, so in theory you can just plugin your existing jQuery and it will just work. It hasn’t worked for me but I think I was being a little optomistic that everything would just magically work still I am going to keep on working with it to see what more I can get out of it.
Have you ever with returning record that are depedndent on time based criteria. One of my last projects had a token based logging in system. A user clicked a login button, it created a token on a remote site and redirected the user away to the other site if the token matched and wasn’t out of date (more than 30 seconds old) then the user was logged in.
We then started to get problems with people just not being able to login. It didn’t take 30 seconds to redirect and recover the token from the database so what was going on.
The database server was on a different server to the web server which hosted both the Main site and the one that allowed token access. However the database server time was about 10mins behind the time on the webserver so Ruby’s Time.now and SQL’s now() or CURRENT_TIME() were returning the incorrect results.
At that point running ‘ntpdate’ to update the server time on the database server worked. But going back and fixing every instance of ‘now()’ was probably the smart move.
There’s a lesson to be learned from that, if you’re going to trust the time then it should be the same across all the servers you are using. But also if we are using an ORM to encapsulate SQL logic then stray into SQL at your peril as you may get unexpected results.