Cómo anular 'dónde' en rieles 3

I have upgraded my application from rails 2.3.8 to 3.0.3 . But I'm facing a problem. I was using 'find' but the overriding doesn't work in rails 3:

# override activerecord's find to allow us to find by name or id transparently
  def self.find(*args)
    if args.is_a?(Array) and args.first.is_a?(String) and (args.first.index(/[a-zA-Z\-_]+/) or args.first.to_i.eql?(0) )

I'm wondering if there is a way to make this work in rails 3 or even by using where instead.


preguntado el 09 de enero de 11 a las 11:01

1 Respuestas

The problem you're are facing is an upgrading from a rails 2.3.X to a rails 3.0.X application. Although, it could seem a simple task it isn't, especially if you have a real application and not a toy one. I suggest you to take a look to a screencast series from Rayn Bates, you could start from http://railscasts.com/episodes/225-upgrading-to-rails-3-part-1 to get a complete idea off the problem you are facing.

If you only need to read about ActiveRecord new interface http://m.onkey.org/active-record-query-interface is a great article.

Respondido el 09 de enero de 11 a las 14:01

Have you ever heard about overriding? en.wikipedia.org/wiki/Method_overriding . my problem isn't in the upgrading. I already passed it. my problem is when I called User.find('any name'). it calls the overriding method which i made in rails 2. but in rails 3 gives me an error says: "Couldn't find User with ID='any name'". because it isn't seeing the overriding method - Zakwan

Yes I do know what method overriding is. I was just suggesting it's not a good solution trying to use it here. I can't think about a good case where you have really use method overriding for the find method of activerecord - lucapette

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.