December 2009
3 posts
2 tags
content_for stores a block of markup in an...
Calling content_for stores a block of markup in an identifier for later use. You can make subsequent calls to the stored content in other templates or the layout by passing the identifier as an argument to yield. F.e. this is quite useful to set the title of your page depending on what view is displayed. In your view you write something like <% content_for :title, "My title" %> ...
Dec 22nd
2 notes
6 tags
Display ActiveRecord generated SQL queries in the...
If you want the console to display the SQL query that ActiveRecord executes just do the following (before you do anything else in the console): $ script/console >> ActiveRecord::Base.logger = Logger.new(STDOUT) => #<Logger:0x10322d6d0 ...> >> User.first User Load (3.3ms) SELECT * FROM "users" ORDER BY last_name, first_name ASC LIMIT 1 Each call to ActiveRecord now logs...
Dec 20th
29 notes
8 tags
all? and any?
With all? and any? you can check, if all or any elements of an Array or Hash (or any other class that includes the Enumerable module) match a certain criteria. The result is either true or false. %w{ ant bear cat}.all? {|word| word.length >= 3} #=> true %w{ ant bear cat}.all? {|word| word.length >= 4} #=> false [ nil, true, 99 ].all? #=> false %w{ ant...
Dec 9th
3 notes