简体   繁体   English

谁能用红宝石向我解释此代码?

[英]Can anyone explain me this code in ruby?

Can anyone explain me the logic behind this code?. 谁能解释这个代码背后的logic

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?. 我是ruby/rails community新手,我无法理解该函数返回的内容以及该函数的整体功能?

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'. 因为该方法以self开头,所以它是一个类方法,这意味着它不会在单个实例上运行,而是“为整个类说话”。 where , when used bare like this, implies that it operates on self, which again, is the class. where ,当像这样裸露使用时,意味着它对自我进行操作,而自我又是该类。

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. 简短的答案:它从此类表示的表中返回第一条记录,该记录与传递的SQL条件以及在条件之后看到的授权条件匹配。

This seems like a part from a rails activerecord model. 这似乎是来自Rails ActiveRecord模型的一部分。

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 小写的用户名或电子邮件必须等于 signin.downcase

the SQL equivalent should be SQL等效项应为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM