I finally found an opportunity to use ruby partition method. It splits an array you have upon a criteria you supply in a block. Typically, in the old days, I would have done this by doing something like this:
big_array = Array.new(11) {|i| i}
number_lower_than_5 = []
big_array.each do |number|
if number < 5
number_lower_than_5 << number
end
end
# >> number_lower_than_5
# => [0,1,2,3,4]
or more recently I would use select
big_array = Array.new(11) {|i| i}
number_lower_than_5 = []
number_lower_than_5 =
big_array.select do |number|
number < 5
end
# >> number_lower_than_5
# => [0,1,2,3,4]
These are both fine methods but what happens if you need the other halve as well? The above approaches just collect you an array to work with and both lack the convenient array of the other stuff you sometimes care about. That’s where ruby’s partition comes in handy. It may not be that often that’s why I was so pumped up when I found an opportunity to use it.
big_array = Array.new(11) {|i| i}
number_lower_than_5, the_rest =
big_array.partition do |number|
number < 5
end
# >> number_lower_than_5
# => [0,1,2,3,4]
# >> the_rest
# => [5,6,7,8,9,10]
When I write web apps I try my best to keep my urls clean and legible. One thing that has always troubled me is how to handle label creation of named things web users key into the app. For example if a user enters a name of a restaurant Alicanté Café the uri safe for of that (if there is no intervention) ‘Alicant%C3%A9%20Caf%C3%A9’, which is total gobblygoo and one might as well show a uid or something of that nature since at least that doesn’t physically hurt the eye. As someone who has worked on a lot of Restaurant related apps this has always been something I wanted there to be an easy solution to. This weekend I decided it was worth trying once again to see what would work best. And after reading the “Internationalization in Ruby” in The Ruby Way. Here is the super simple trick
require 'iconv'
strange_name = "Alicanté Café"
converter = Iconv.new('ASCII//TRANSLIT', 'UTF-8')
converter.iconv(strange_name)
"Alicant'e Caf'e"
It leaves some odd ’ and “sfor some reason but they will get stripped away with all the other potential weird stuff out (”,’,etc). Still this discovery makes me stop complaining about pretentious foreign sounding restaurant names.
A while back I decided to get fancy and start hosting this blog on ec2 cloud just to sort of get the hang of working on that kind of a system. This of course involved moving database over and the works. Everything seemed to be going fine until I started posting… things were just busted bad. I kept getting
ActiveRecord::StatementInvalid (Mysql::Error: Duplicate entry ‘0’ for key 1
errors and for the longest time I just ignored it since I was able to post by using a some of my masterpiece drafts that I wasn’t going to use any how… but then I ran out of those and I felt an urge to post but that meant I needed to tackle the problem and as it turned out it was a simple fix after I had done the research. The primary key fields had lost the AUTO_INCREMENT magic during my db move and the solution was simply to go in and ad them where a appropriate.
ALTER TABLE `contents` CHANGE `id` `id` INT NOT NULL AUTO_INCREMENT;
Boy is that place a rats nest… haven’t looked at the site for a year now and decided to see how long it would take me to upgrade to rails 2. Remarkable enough it was really not that tricky to do which speaks to the level of my knowledge of rails at the time which was fairly slim so all my upgrading tasks where fairly light. So to make things more exciting I decided make it more rest-y … which also was remarkable light task except for a few minor issues such as lack of tests which makes it terribly difficult to even know if the site is working at all except for the fact that I clicked around a bit … (god I wish I had known testing better a year ago). So before I would dive into upping the test coverage I decided to save the very little traffic I get from google by issuing a 301 for the urls that changed. So as an former mod_rewrite ninja (oh did I mention that I deployed the site before I fell in love with nginx) I fired up .htaccess and wrote all the rules for the rewrites … then after 30 minutes of cursing and moaning I read somewhere that .htaccess doesn’t take on rails app jez. Not to worry I just stuffed the rules into my virtualhost config and things are flying.
One cool thing I learned today was that you can set the columns when you generate the rails migration from the terminal. The prepopulates the tabel generation code with the table names and datatype. This isn’t such a big find since it’s well documented in script/generate model—help. But here is how it looks.
script/generate model members name:string age:integer
script/generate model portfolio member_id:integer body:text
Another thing I just came to realize is that defining explicit foreign_key constraint to the database is probably a good idea. I don’t know too much about database administration but if I understand it correctly it helps with dataintegrity and probably performance if the table grows big. Adding indexes is also a plus. A little specifity never hurt anyone. This example is for MYSQL with the InnoDB engine which I believe is the default for rails now.
. . . def self.up create_table :members do |t| t.column :name, :string t.column :age, :integer end end . . . def self.up create_table :portfolios do |t| t.column :member_id, :integer t.column :body, :text end execute 'ALTER TABLE portfolios ADD CONSTRAINT fk_portfolios_member \\ FOREIGN KEY (member_id) REFERENCES members(id)' end . . .
I have read many great books on web development. Books that have been essential in honing my skills and helping me do my job with more confidence. But there is a diminishing return effect when adding a book to your tech book collection. These books always contain material that becomes increasingly less important for you, things such as the out of date install chapter, followed with short syntax overview where you get examples of how to assign a “hello world!” string to a foo variable. It looks to me that publishers basic assumption is that you are picking up a programming book is for the fist time.
The more in demand and popular web development gets the quality of the books on it suffers, mostly written by professional book writers some of who haven’t done actual development in years. It astonishing how absent books with real contemporary experiences straight from the trenches of web development are on the bookstore shelves. This is the information one tries to sniff out from blogs, case studies and online material, stuff that isn’t hold hostage by the editorial process, market research etc. But nothing really beats the book format as old fashioned that sounds. So ideally you would get all of the good stuff in a book format with none of the downsides, and how about just paying $10 in stead of the regular $50? Well, now you can.
Geoffrey Grosenbach, well known in Ruby on Rails community for his podcast and screencasts, has started his own publishing adventure PeepCode Press Manifesto and now has released a draft of his upcoming book Code Review. The book is filled with many good advice aimed to arm the novice developer with veteran information to avoid various pitfalls of Ruby on Rails web development. Configurations that are rubbish by default are mentioned in addition with lots of tips for optimization. I wish I had read a book like this earlier in my Ruby on Rails learning curve because that would have saved my a lot of embarrassment.
Code Review has an rather unusual approach it starts by telling you how to do it wrong and then goes on to correct that. This works because well because it is consistent through out so there isn’t any risk of confusing the wrong with something being right as is often can be the case. I am really looking forward to reading more books off the PeepCode Press! I hope they will all be as elegantly designed and articulate as Code Review is.
Over all it’s a great draft and you should go get it now, no joke
Usually when you think you are doing everything right, when it comes to protecting sensitive data. You might even brag to your friends how smart you were saying that you have all password securely stowed away in your user database as hash values generated with salt through some crazy algorithm, and not as clear text. You tell them that there is no way to retrieve, a forgotten password, and the only solution is to reset it. But are you sure? have you looked at your log files?
As I was going through my production.log looking for optimize opportunities I noticed that the values of the fields posted to my rails application where diligently being logged, which is alright for most things but not passwords and other things you don’t want the liability to know about, so you never want them stored in clear text. After panicking for a little while I quickly realized that there was a quick solution as always when you’re developing in Rails, filter_parameter.
class someController < ApplicationController
filter_parameter_logging :password, :password_confirmation
def some_method
. . .
end
. . .
end
Although I feel a bit ashamed not having known about this security issue it made me feel a bit better that my rather recent download of mephisto 0.7.3 also stores passwords in the log file. So I guess this even happens to more experienced developers. P.S. Mephisto is really awesome.
—Update [8/5 2007] the most recent trunk of Mephisto this had been fixed.
This is perhaps yet another reason to adopt openid! Assuming the provider you choose knows about all of these pitfalls in what ever technology they use.
I have been seeing a lot of this mephisto and it seems that all the cool kids I look up to use it so I wanted to get on the wagon and install and use it too. And I have to say I have no regrets it’s so much more fun to use than wordpress and the best part is it’s ruby on rails so there is less php that I have to deal with.
Now that everyone who is doing web development is supposed to be having so much fun! All the latest technologies are all about removing the pain and putting in the fun … but wait! How about making deploying all of this stuff fun too? It’s easy to throw up Ruby on Rails and run it on your local machine on the good old http://localhost:3000. But what about deploying it to your just recently purchased vowel free godaddy domain which sounds so 2.0?
When I initially started researching how to deploy Ruby on Rails application I got scared shit-less fast-cgi, mongrel, apache mod_proxy and god knows what. I spent a good part of a Saturday trying to install and set this up on a virtual host that I had at the time but soon gave up and started googling for easier answers, even considered for a brief moment paying ridiculous amounts for someone to do it for me. And like with so many things I was not the only one who had gone through this and there are always smart people out there that help the rest of us to achieve the seemingly impossible. Here is a list things and places to consider if you are in, or want to be in the process of deploying Ruby on Rails application.It’s amazing how something that otherwise seems very complicated can become manageable just by tapping into the right library, grabbing some sample code and add the little flare that excites you. That’s the gist of my experience implementing openid on grubsnitch.com, an experimental community website. Ever since I first heard about openid I wanted to support it on the site. For a small site reluctance to signup to “yet another site” is the biggest hurdle in acquiring new users. Now with openid the users can choose to trust me with their password information or simply have those responsibility handled by their openid provider.
If you are interested in implementing it on your site you should check out Dan Webb’s No Shit Guide To Supporting OpenID In Your Applications. I used that tutorial as a base for my implementation but added the ability to have regular users to live in harmony with openid users. I am working on extending Dan’s tutorial to include those features which will also have the potential of allowing users to change how they authenticate with the site according to these two use cases.