theJava
theJava

Reputation: 15044

Can anyone explain me this code in ruby?

Can anyone explain me the logic behind this code?.

def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup
    where(conditions).where(["lower(username) = :value OR lower(email)
    = :value", { :value => signin.downcase }]).first
end

I am whole new to the ruby/rails community, i am not able to understand what is returned by the function and what overall this function does?.

Upvotes: 0

Views: 124

Answers (2)

Daiku
Daiku

Reputation: 1227

What is returned is an active record relation - in this case, a single record. Because the method starts with self, it's a class method, which means it does not operate on a single instance, but rather 'speaks for the entire class'. where, when used bare like this, implies that it operates on self, which again, is the class.

Short answer: It returns the first record from the table represented by this class, that matches the SQL conditions that were passed, and the authorization conditions you see after the conditions.

Upvotes: 2

xlembouras
xlembouras

Reputation: 8295

This seems like a part from a rails activerecord model.

it is a class method that returns the first record that meets the given conditions provided as:

  • warden_conditions
  • the downcased username or email must be equal to signin.downcase

the SQL equivalent should be

SELECT * FROM items WHERE conditions_to_sql AND lower(username) = a_value OR lower(email) = a_value limit 1

Upvotes: 1

Related Questions