Notes posted by metavida
RSS feed
RecordNotFound when any id not found
As an example of bansalakhil’s explanation.
User.find_by_id(1) #=> #<User:0x3d54a3c @attributes={"id"=>1}> User.find_by_id(2) #=> #<User:0x3d519a4 @attributes={"id"=>2}> User.find_by_id(3) #=> nil User.find_by_id(1, 2) #=> an Array with 2 User instances User.find_by_id(1, 3) #=> an ActiveRecord::RecordNotFound error

:confirm, :popup, and :method override :onclick
upplying any combination of :confirm, :popup, and/or :method options to the link_to method results the :onclick option being overridden.
Example:
link_to "Delete", '#', :confirm=>"Are you sure?", :onclick=>"destroyJsFunction()" # expected output # => <a href="#" onclick="if(confirm('Are you sure?')) {destroyJsFunction()}; return false;">Delete</a> # actual output # => <a href="#" onclick="return confirm('Are you sure?');">Delete</a>
Note that the actual output doesn’t include any mention of the “destroyJsFunction()” passed to the link_to method.
Rails 3 will use unobtrusive JavaScript, and I haven’t tested how that will interact with the :onclick option.

Looking for "to the power of"?
If you’re trying to calculate 2 to the power of 2, the ^ method is not what you want. Try ** instead.
2^2 #=> 0 2^8 #=> 10 2**2 #=> 4 2**8 #=> 256

File name without the extension
To get the file name without the extension you can use File.extname in combination with File.basename
File.basename("test.rb", File.extname("test.rb")) #=> "test" File.basename("a/b/d/test.rb", File.extname("a/b/d/test.rb")) #=> "test" File.basename("test", File.extname("test")) #=> "test" File.basename(".profile", File.extname(".profile")) #=> ".profile"

For the file path use File.dirname
File.dirname provides what File.basename omits.

Wrapping peculiarities as of 2.x
In Rails 2.x word_wrap has been improved so that it no longer consumes multiple line-breaks or leading & trailing line-breaks.
word_wrap("\nOnce upon a time\n\nThe End\n") # => \nOnce upon a time\n\nThe End
However it still doesn’t break long words
"supercalifragilisticexpialidocious".length # => 30 word_wrap("\nOnce upon a supercalifragilisticexpialidocious time", 15) # => \nOnce upon a\nsupercalifragilisticexpialidocious\ntime

Wrapping peculiarities
word_wrap will consume multiple line-breaks as well as leading & trailing line-breaks.
word_wrap("\nOnce upon a time\n\nThe End\n") # => Once upon a time\nThe End
word_wrap will NOT break long words
"supercalifragilisticexpialidocious".length # => 34 word_wrap("\nOnce upon a supercalifragilisticexpialidocious time", 15) # => Once upon a\nsupercalifragilisticexpialidocious\ntime
If you want a function that will break long words & maintain multiple line-breaks try this alternative. Note it does add a line break at the end of the output.
def breaking_wrap_wrap(txt, col = 80) txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/, "\\1\\3\n") end breaking_wrap_wrap("\nOnce upon a supercalifragilisticexpialidocious time", 15) # => \nOnce upon a\nsupercalifragil\nisticexpialidoc\nious time\n
Regex-based code from http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/

print standard-looking messages during migration
Within a migration file you can use the say_with_time method to print out informational messages that match the style of standard migration messages. See the say method also.
say_with_time "migrate existing data" do # ... execute migration sql ... end #=> "-- migrate existing data" #=> " -> 0.0299s"

print standard-looking messages during migration
Within a migration file you can use the say method to print out informational messages that match the style of standard migration messages. The say_with_time method is also pretty great.
say "migrate existing data" #=> "-- migrate existing data" # ... execute migration sql ... say "updated all records", :subitem #=> " -> updated 5 records"