After you finish the Rails tutorials and start your own app, things get confusing something like where does your non-CRUD and general logic go?
You can’t build the app you’ve been dreaming of without some general, non-Rails logic. So where do you put your code, and still keep things simple?
The Easy Place to Start
when I have logic that feels related to an existing ActiveRecord model, I’ll start by putting it into that model. For example if I had a Game model and I want to import a bunch of games from CSV files, I would put that method right onto the Game class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
You have all the information your methods need right at hand. it’s easily testable. And it’s probably where a new contributor world look for that logic first.
But if you keep adding and changing that model, it’ll get big and complicated. Different parts of the model will interact in strange ways. The more you change it, the harder it will be to change.
In that case, you would probably want to refactor that code out to a non-ActiveRecord model.
You can write your own Ruby code, in plain Ruby objects, and use them in your Rails app. These objects can still be called models, because they’re modeling part of your problem. They just don’t have an ActiveRecord database storing their data.
The next time I worked on that game CSV parser, the Game class was getting a little too big. So I moved the parser logic into its own GameCSVParser class.
Non-ActiveRecord class looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
I will go right to creating a new plain Ruby object if the logic I’m adding doesn’t feel related to any specific ActiveRecord model. Of if the code seems like it should be a part of a thing that doesn’t exist yet in the app. Otherwise, they mostly pop up through refactoring.
So far so good, That’s it!!! See ya!!! :)