Paginating grouped records

jamesconroyfinn Jul 29, 2009

If you are grouping similar records and paginating you might need to use :group You'll want to :select only the field you're collapsing on probably.

Model.count(:select => :attribute, :group => :attribute)

This will return an OrderedHash of your attributes with a count for each.

{"Column Con...

Return True

actsasflinn Jul 28, 2009 8 thanks

As is the case with the before_validation and before_save callbacks, returning false will break the callback chain. For example, the expire_cache_id method will not run if Rails.cache.expire returns false (as it will if the key is not cached with memcache).

=== Returning False Example (Bad)

a...

Instance method

szeryf Jul 24, 2009 1 thank

Please note that this is an instance method, not a class method (which seemed more logical for me and took me a while to see what's wrong). So, you call it like this:

User.new.from_json '{"id": 1, "name": "DHH"}' # RIGHT!

not like this:

User.from_json '{"id": 1, "name": "DHH"}' # WRONG!

Format meaning:

annaswims Jul 23, 2009

%a - The abbreviated weekday name (``Sun'')

%A - The full weekday name (``Sunday'')

%b - The abbreviated month name (``Jan'')

%B - The full month name (``January'')

%c - The preferred local date and time representation

%d - Day of the month (01..31)

%H - Hour of the day, 24-hour clock (...

Format meaning

annaswims Jul 23, 2009 7 thanks

%a - The abbreviated weekday name (``Sun'')

%A - The full weekday name (``Sunday'')

%b - The abbreviated month name (``Jan'')

%B - The full month name (``January'')

%c - The preferred local date and time representation

%d - Day of the month (01..31)

%H - Hour of the day, 24-hour clock (0...

Highlight keywords in a text

zoopzoop Jul 17, 2009 1 thank

Case-insensitive

keywords.inject(text) { |text, keyword| text.gsub(/(#{keyword})/i, "<strong>\\\\1</strong>") }

can be replace by whatever HTML tag you want for hightlighting (, , ...)

Any base logarithm

szeryf Jul 13, 2009 1 thank

Using basic arithmetic you can get logarithm with any base:

def log_with_base base, num
Math.log(num) / Math.log(base)
end

Examples:

>> log_with_base 2, 10
=> 3.32192809488736
>> log_with_base 2, 2
=> 1.0
>> log_with_base 2, 4
=> 2.0
>> log_with_base 2, 16...

Any base logarithm

szeryf Jul 13, 2009

Using basic arithmetic you can get logarithm with any base:

def log_with_base base, num
Math.log(num) / Math.log(base)
end

Examples:

>> log_with_base 2, 10
=> 3.32192809488736
>> log_with_base 2, 2
=> 1.0
>> log_with_base 2, 4
=> 2.0
>> log_with_base 2, 16...

Using block version in Ruby < 1.8.7

Mange Jul 8, 2009 5 thanks

The block usage was added in 1.8.7, so to get the same functionality in an earlier version of Ruby, you need to utilize the find method.

Here is a quick example:

match = list.find { |l| l.owner == myself }
match_index = list.index(match)

If you do some gymnastics, you can have it on one l...