Wednesday 28 March 2012

Stumbling blocks

I've been bumbling through a few self-made hurdles the last few days and they all unraveled when I sat an unknowledgeable friend next to me and spoke at him as I worked through my problem. This is what I call the rubber duck method.

New knowledge 1)
@thing.user_id = User.find_by_email(params[:legitimate_user_email])
The flaw here is that I this caused me a really hidden error.
I thought assigning it to a user would use the ID since that is how the models for each of these is linked together, but instead it kept returning ID1 instead of throwing an error.
@thing.user_id = User.find_by_email(params[:legitimate_user_email]).id
WOW that was a simple flaw but it didn't show up because it threw no error.
I was almost defeated by 3 letters and I have never been so embarrassed.

New knowledge 2)
Redirect_to :back
the :back parameter is really cool. I was passing a param back to the controller to know where to redirect my users back to, which was causing me to use POST and GET params at the same time, when I could have used :back all along. Code is now prettified and users now cannot redirect themselves to nasty places using an obvious param in the url.

New knowledge 3)
rake db:drop db:create db:migrate db:seed db:test:prepare
I should have a macro for this or something. Saves me time and attention knowing I can chain rake tasks now. Minutes are saved every day.

New knowledge 4)
before_filter do |c|
    c.send(:authorize, "admin")
end
Passing params to the before_filter method is really quite awesome and very helpful if you are writing your own permissions from scratch. I had no idea what I was going to do to get round this as there was no way I was going to explicitly name every method. I would miss my deadline by months even attempting it.

New knowledge 5)

require 'rubygems'
require 'factory_girl_rails'
Loading a gem for local use in a file is not only useful when you want to use a gem outside its intended scope, but apparently there are optimization benefits in loading the gem only when needed too! (according to someone on stack overflow, but I haven't tested it and he also said it was a maintenance nightmare!)

New knowledge 6)
Rails.root.join('folder','file_in_folder.rb')
The cross platform version of  "%Rails_root%/folder/file_in_folder.rb"
Both allow you to rummage around anywhere inside of your own rails directory but the first works on operating systems that do not use '/' as a separator.

Altogether, I'd say this has been a massively successful week. I've been using I18n.translate aka t ("some.location.in.config.locale_name.yml") which is extremely cool, and I'm expanding my use of capybara and TDD.
Now I write it down, I am learning quite a lot in a short amount of time.

Edit:
New 7)
I didn't think it was from this week as it seems so long ago, but commit logs say otherwise.
<%= collection_select("group", "permission_id", @permission, "id", "name") %>
Selecting from a collection is fun, I'm not sure if it is badly documented or I was looking for all the wrong keywords.

No comments:

Post a Comment