#1 new
Jérôme

Why not extending the find method ?

Reported by Jérôme | June 12th, 2008 @ 01:44 AM

  # Default parameter can be slug or id
  def self.find(*args)
    if args.first.is_a?(String) && !args.first.numeric? && args.first !~ /^\d+-/
      find_by_param(args.shift,*args) or raise ActiveRecord::RecordNotFound
    else
      super
    end
  end

Comments and changes to this ticket

  • Jérôme

    Jérôme June 12th, 2008 @ 01:46 AM

    oh, numeric? doesn't exist in rails:

    class Object
      def numeric?
        false
      end
    end
    
    class Numeric
      def numeric?
        true
      end
    end
    
    class String
      # "1".numeric?
      # >> 1
      # "1 ring".numeric?
      # >> false
      # "1,23".numeric?
      # >> 1.23
      def numeric?
        if self =~ /^\d+$/
          self.to_i
        elsif self =~ /^\d+([,\.]\d+)?$/
          self.tr(',','.').to_f
        else
          false
        end
      end
    end
    

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

A Rails plugin addition to ActiveRecord::Base that establishes a better
convention for finding records based on parameters.

People watching this ticket

Pages