Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best method sequence for Ruby on Rails model? [closed]

In Rails, we offen write a lot of code in our models such as class methods, plugin methods, named_scopes, callbacks... I am wondering if there is a good pattern to organize the sequence. I saw the best example in a presentation before, but now I forgot.

Anyone have suggestion? Thanks

like image 313
xdite Avatar asked Apr 25 '26 07:04

xdite


2 Answers

There is no set way... If you are using a scope (since Rails 3, named_scope is deprecated) that relies on a method, it has to be defined after the method in the model. It's possible to mix and match and sometimes it's necessary to do so.

It doesn't affect load time or efficiency to the best of my knowledge

like image 159
sethvargo Avatar answered Apr 27 '26 04:04

sethvargo


I'm pretty OCD when writing ruby, so I have a very opinionated answer to your question. I created this gist as an example of the structure we use.

like image 28
BM5k Avatar answered Apr 27 '26 03:04

BM5k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!