Notes posted by joshuapinterRSS feed
Client.where( country: 'Canada' ).update_all( country_code: 'CA' )
(or any regular expression you’d like)
'Abracadabra'[0..0] =~ /[A-Z]/ # => true
Useful for when you need to create a Date object from these manually, such as for reporting.
If the date select input is start_date and it belongs to the report form object:
@start_date = Date.civil(params[:report]["start_date(1i)"].to_i, params[:report]["start_date(2i)"].to_i, params[:report]["start_date(3i)"].to_i) @start_date.class # => Date @start_date # => Sun, 25 Sep 2011 # For example.
Use a similar method for DateTime situations.
Thought it would be best to take the source code from pluralize and just remove the count from the output.
Create this helper method in application_helper.rb
# Pluralize without showing the count. def simple_pluralize count, singular, plural=nil ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : (plural || singular.pluralize)) end
This allows you to pass in in the plural word to use as well.
I thought using to_formatted_s(:db) on an array of ids would separate them with commas in a nice way. Wrong. It does, but it also changes the numbers.
[60, 271, 280, 283].to_formatted_s(:db) # => "121,543,561,567" # Completely different numbers!
Instead, use the join method:
[60, 271, 280, 283].join(",") # => "60,271,280,283" # Much better
I think this has to do with (:db) being used for formatting dates but I’m not sure.