Guru on Rails
In fact, Ruby on Rails (ROR) does help us a lot. It makes our code shorter. It's close to the natural language. The syntaxes are pretty nice. I can't agree more about these things. However, as a Ruby on Rails developer if we don't really know exactly what are happening under the ActiveRecord, it would be a nightmare when the application evolves time by time. System is actually getting worse but we don't notice.
As a ruby on rails developer we need to keep practising every day about SQL. Because, in my opinion we don't usually use SQL. ActiveRecord provided us convenient ways to deal with database. If we just use it, we will forget SQL syntaxes and losing problem solving skills on database sooner or later.
Nowaday, people are trying to invent many things to support Human being. I can see that people who inherit the results become more lazy. If we have a productive tool, we're going to do things by our hands? Ruby on Rails provided a productive tool like that. ROR is good, I can say that. It'll be tough when we overuse it, when we lose the control of logic behind the scene.
Author.includes(:books).where("books.page > 200")
Did you know that "includes" uses eager loading whereas "joins" uses lazy loading?
Author.joins(:books).where("books.page > 200")
Two above queries do the same thing. That is to query "INNER JOIN", get all of authors and books which have more than 200 pages. However, which case we should use "includes" and vice versa.
In fact, using "includes" help us to avoid the N+1 queries problem in rails. Because we load all of data in only one query (eager loading). In case of using book's attributes, it will benefit:
authors.each do |author| puts author.books.count # need to use books data, use includes more better. end
However, if we don't need to access to books data, we should use "joins". Because "joins" uses lazy loading. We don't need to load all of books data, do we? It just wastes the memory.
authors.each do |author| puts author.name # we don't use data of books end
Unluckily, beginners or even junior developers don't get enough attention on this. As a ROR, we use ROR wisely. We do fast, take advantage of our weapons, but we know exact what we are doing, no more, no less. Let's learn from the previous generation, learning through the best practices and design patterns. No one can list all of problems for us to avoid. Hence, we find them out, seeking any problems, any questions related to ROR and to what we're being concerned as well.