-1

I am new to ruby and rails development and have this question in mind. If i have a concern with scope created say latest_records which gives me latest data for a customer

Now what is the best practice to use scope in this situation. should scopes be in model or in controller?

I read some online articles and it talks about fat model and skinny controller and since scopes are do database related work then i am guessing they should be in model.

Any suggestions or thoughts?

User7354632781
  • 2,174
  • 9
  • 31
  • 54

2 Answers2

3

You guessed it right. Scopes belong to model and needs active record classs object to work on. Scopes are nothing but a activerecord query divided in parts and it helps look your query elegant and dry. e.g. If you want to get users with confirmed emails, you would:

User.where(confirmed: true)

But with scopes in your user model:

scope :confirmed, -> { where(confirmed: true) }

And you would simply:

User.confirmed

For more detailed please refer this answer here

Community
  • 1
  • 1
Md. Farhan Memon
  • 6,055
  • 2
  • 11
  • 36
1

A scope could only be defined in a model so it would have to be on a model.

timpone
  • 19,235
  • 36
  • 121
  • 211