Funny things about Rails scope and class method

Duong Bach viết ngày 29/12/2015

Rails scope and class method

When I first read about scope:

All scope methods will return an ActiveRecord::Relation object which will allow for further methods (such as other scopes) to be called on it.

That's means : scope is chainable (as far as i understood it)

okay, then class method must be different

Assume that we have a well defined, usable class User

User class had game_id and site_user_id

Let us implement some simple scope and class method in order to test

alt text

OK, we have a scope named scope_game_id and a class method named class_method_game_id with the same purpose: find user by game_id
as shown below
alt text

alt text

okay, now test the CHAINABLE

first the scope

alt text

Oh, it does chaining two conditions together, making one beautiful SQL

How about the class method, I wondering ...

alt text


The class method also can chain the conditions together, working exactly the same with scope

okay, Let do some mini test

  • nil input?

alt text


  • scope allow error input while class method doesnt ?

alt text

no luck ...

  • output nil

alt text


So what is the true purpose of SCOPE ???

maybe, scope's purpose is just for FUN, make source code looks better ?

u're kidding me?

Luckily, I run the lastest test
Actually I had some chances in my code by adding .first

alt text

alright, one more time

alt text

the class method gives an nil result

how 'bout the scope

alt text

wow, how come it still output records ?

If we take a closer look, after the main logic query, scope automatically check the result, if the result is nil , the magic appears

SELECT `users`.`*` FROM `users` ORDER BY `users`.`id` ASC LIMIT 1

and of course with the help of magic, there will be no nil

So the CHAINING does makes sence here...

The chaining cant to continue if there is a broken chain

Scope will make sure the broken chain wont exist by return all the records when the searching result is nil while class method won't

This make scope a really useful feature , but BE CAREFUL using it

Actually, I had to fix a bug today, so I accidentally found this interested.Before that, I doesnt really care about these things.
And I means it

BE CAREFUL using it

but why has to be careful ?
because of the bug?
I dont really think so...there are things more annoying than the bugs

okay, I will do an research on "Where and When to use scope and class method before I can tell anything about that...

See you in the next part.

Bình luận

{{ }}
Bỏ hay Hay
Male avatar
{{ comment_error }}

Hiển thị thử

Chỉnh sửa


Duong Bach

2 bài viết.
0 người follow
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
4 0
Ruby Object copy Lately I've faced some Ruby Object Problems Actually this is the OOP object copy problem.But I dont know much about OOP so... Ok,...
Duong Bach viết hơn 2 năm trước
4 0
Bài viết liên quan
8 6
Chưa xem phần 2? Xem (Link) Trong bài viết này tôi giới thiệu cho các bạn về khái niệm function arity, một cách gọi mĩ miều của số lượng argument ...
Lơi Rệ viết hơn 2 năm trước
8 6
8 1
Tiếp theo (Link) Mình sẽ hướng dẫn cách test căn bản cho API mình tạo. Thật ra mà nói thì mình phải viết test trước khi làm nhưng mà để tránh việc...
My Mai viết hơn 2 năm trước
8 1


{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}

{{userFollowed ? 'Following' : 'Follow'}}
2 bài viết.
0 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!