What is scope/named_scope in rails?

前端 未结 5 1056
耶瑟儿~
耶瑟儿~ 2020-12-02 04:08

I\'ve recently started an internship. My employer uses ruby on rails, and I frequently encounter new syntax that I need to look up to understand. I\'ve googled around for a

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 04:58

    • Imagine you have a model: Person.

    Now imagine you :

    • want all the people in the world who have red hair.
    • want all the people in the world who play cricket

    You could get those particular classes of people by using a scope!

    Person.red_hair.cricket ## finds all people with red hair who play cricket
    Person.red_hair ## finds all people with red hair
    Person.cricket ## finds all people who play cricket.
    

    Now that wasn't so hard was it?

提交回复
热议问题