My take on the 7 reasons...
So the hottest topic in the rails community is seems is Derek Sivers’ blog post on why he switched from trying to reimplement his site in rails to php.
As someone who writes both rails and php for a living I feel that I have a good perspective on the rails vs php thing.
I’m sure the arguments will continue for some time, but all I know is that I write much better php since working with rails.
Right tool for the job and all that.
Upgrade to Typo 4
I have upgraded my blog to Typo version 4 and it went mostly seamlessly.
The box that hosts this blog is running plesk which wasn’t a problem.
I tried just installing the new version of typo over the old 2.6 version and while this appeared to work it caused some problems, notably with the migrations.
In the end I deleted my old typo install and ran these commands:
This box is running Centos 4 . I had sqlite installed but not the header files. These are required to get the sqlite gem files to install which are required by typo.
yum install sqlite-devel
Install the typo gem:
gem install typo
Install typo in my webroot:
typo install /var/www/PATH/httpdocs
The next command should have changed the configuration from mongrel and sqlite to apache and mysql but it didn’t work for me for some reason:
typo config /var/www/PATH/httpdocs web-server=external database=mysql
I had to manually change the settings in database.yml and I also had to create a .htaccess file in the public folder to get fastcgi working.
Then
rake migrateto update the database tables.
I have moved the blog a few times over the last year or so and along the way various things got broken, so it is nice to have everything working again.
[snippet] Cleaning up currency strings
I was working on some data that I needed to import into mysql for a new rails project I’ve started.
Some of the fields are currency strings, eg £4,000.00. Now initially I was tempted to just bung them into the table as varchars but there is a good chance that at some point down the line I’m going to want to ‘do maths’ with these numbers so I really had to convert them to floats.
In the end I used this little bit of code which worked a treat:
return amount.gsub(/[£,]/,’‘)
So what this does is matches the characters £ and , and substitutes them with blank. Works a treat.
Rails Express blog
The rails express blog written by Stefan Kaes, author of railsbench is a great resource for anyone interested in wringing a bit (or a lot in some cases) more performance out of Ruby on Rails.
Some great tips on there.
Good stuff.
Rails, Mysql native bindings 2.7 on Centos
This post over on the RoR blog a few days back talks about upgrading to the new mysql native bindings for ruby to get the best performance.
However, I’ve been unable to get it to install using gem, a la :
gem install mysql
as I have been getting compile errors:
ERROR: While executing gem - (RuntimeError) ERROR:
Failed to build gem native extension.
Despite the number of solutions to this out there, I’ve only now found one that works with my particular setup, namely centos 4.2, and that can be found in this post
I should point out that I didn’t need to edit the makefile or anything, I just ran make, make install in the
/usr/local/lib/ruby/gems/1.8/gems/mysql-2.7
folder.
All working now, which is nice.
How to count the number of records in a table...
Want to find out how many records there are in a table?
You could do a table.<a href="http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000674">find_by_sql </a> "select count(*) from table",
You could get all the records from the table and then count them, say records = table.find(:all).
Or you could do count = table.count.
How cool is that!
ruby on rails country_options_for_select helper
So I’m still working away on this project using Ruby on Rails, and generally I’m loving it. It really is a great piece of software. Progress is good, but I often find myself stumbling on what should be the simple things.
Today for example, I wanted to use the country_options_for_select() form helper to generate a list of countries as a dropdown in a form, with a number of countries prioritised at the top of the list.
The list was being generated but the prioritised countries where not be generated. The code I had was something like this:country_options_for_select(priority_countries = ['United Kingdom','United States']).
Now the method signature in the api docs is this: country_options_for_select(selected = nil, priority_countries = nil), and I assumed that the selected = nil didn’t need to be included as it was the default.
Not so. Once I added the selected = nil argument to the method it worked.
And the lesson is?
Assumption is the mother of all feck ups I guess.
Formatting dates in rails templates using strftime()
Recently I needed to be able to format a date that I had retrieved from the database into something more readable. In smarty one would do something like $date|date_format but I could not find anything like that in ruby.
Then someone pointed out that the date field in the database would be recognised as such and would be a date object in my View.
As the date is a Date object I can use strftime() to reformat the date any way I want to.
eg. @event.start_date.strftime("%d %B %Y")
How to find the id of a record you have just created in ruby on rails?
How do you get the id of a record you have just created in Ruby on Rails?
Well, the answer to this question came up on the rails mail list recently.
It goes something a little bit like this:
@text.save //insert something into the database
@text.id //get the id of the new record