How to count the number of records in a table...

Posted by paul Wed, 28 Sep 2005 10:30:55 GMT

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

Posted by paul Thu, 22 Sep 2005 23:21:55 GMT

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.