0

Is there a specific reason I need the self in this model's method? I'm accessing it from another controller.

  def self.search(query)
    if query
      find(:all, conditions: ['lower(first_name) LIKE ? OR lower(last_name) LIKE ?', "%#{query.downcase}%", "%#{query.downcase}%"])
    else
      find(:all)
    end
  end
jdoe
  • 15,665
  • 2
  • 46
  • 48
locoboy
  • 38,002
  • 70
  • 184
  • 260

1 Answers1

4

In ruby, self is used for class methods (as opposed to instance methods), which are equivalent to static methods in other languages.

Example:

class Wtf
    def omg
    end
    def self.lol
    end
end

Wtf.new.omg
Wtf.lol
Gareth
  • 133,157
  • 36
  • 148
  • 157
nurettin
  • 11,090
  • 5
  • 65
  • 85
  • Also see previously asked exact duplicate: http://stackoverflow.com/questions/386115/to-use-self-or-not-in-rails?rq=1 – nurettin Jul 05 '12 at 07:00