Here are 10 things I recommend entry-level developers always consider when building an application with Ruby on Rails.
I know. I’m dying to code. But, there is important groundwork to be done before getting started. There are two reasons for this:
The first thing is more like a business issue. Before starting to write code, you must consider,
The second reason is more of an engineering matter:
The last point is often overlooked
Writing pseudocode helps you,
These notes are important because just a few days away from a project, and you can forget why you made some of the decisions you made.
Below is and example of pseudocode (source). What I focus on is,
Ruby is great in that it has a bunch of external libraries but, the more you use them, the more complex dependencies become. Code gets harder to fix when you want to update versions.
When I want to use external libraries (not just Ruby gem) I check
Try to put general modules out of a specific controller.
I think it takes a little time to get used to for people who are new t to programming, but this helps you to fix bugs more easily.
Here is an example from when I created an app to visualize a food menu. I split a task using Google Cloud Vision API inside
app/services/ folder and tried to keep those features apart from controllers.
You can see a better explanation about this here.
Ruby on Rails ships with everything you need to prototype your application quickly, but when your codebase starts growing, you'll run into scenarios where the conventional Fat Model, Skinny Controller mantra breaks. When your business logic can't fit into either a model or a controller, that's when service objects come in and let us separate every business action into its own Ruby object.
This is really sort of a corollary to the one above. Don’t put any method-like code in your view.
MVC model stands with the concept to split tasks to make a large application more simple and easier to maintain. View is for outputting some value of the object, so it’s recommended to separate logic, especially when that can be used among multiple files.
This is where helper method comes in. You can write a method inside
app/helpers/xxx_helper.rb file and call it directly from
.html.erb file. ( Some other code needed in Rails 4)
The sample code shows two ways to output exactly the same thing (the title is shown only first 17 letters, if it exceeds the limit, replaced by … ).
Basically helpers in Rails are used to extract complex logic out of the view so that you can organize your code better. I've seen two benefits for using helpers in my experience so far: Extract some complexity out of the view Make view logic easier to test Let me explain each one of those points.
You might use “the same” visual templates — components such as navbar, sidebar, cards etc… What does “the same” mean in your source code? Yes, you don’t have to write them twice (or you shouldn’t !). You can pack general html into
app/views/shared/ directory and use it from anywhere with
Rails Guide is my first choice as a reference.
The old saying, “there’s no rule without exceptions,” is not only applicable to the business world. Even if you make a list of possible cases all day long, there will always be others . I don’t mean you should address every single possible error (but professionals do). Instead, you’d better decide what to do when errors occur.
Ruby, of course, has a way for exception handling. It’s called
begin/rescue (basically the same as
So, what can you do to improve performance? This question is particularly relevant when you deploying for free since resources can be lacking.
One thing you can do is to make fewer queries to the database. Although deciding whether we should make calculations in the app server or database server depends on the case, making queries is an ‘expensive’ action (factor of slow speed).
Write the SQL query first and then execute it. You can get an idea from this example:
ActiveRecord::Base.connection.select_all(sql) would be the choice, but
Model.find_by_sql() is the easiest to use.
Bear in mind, you should use an index with columns frequently referenced.
Most of the time you’ll create an app with a team. There you manage all of the members’ progress at the remote. (Using Github is most common. Some of you just use Git with a production server on-premise).
I won’t show you specific code for each action in detail here, but one thing I will say is that before you really push your branch to remote (often when you’re filled with a sense of accomplishment ), run
git fetch origin, then
git merge master and resolve any potential MERGE|CONFLICTs.
It’s always a pleasure to push your work to remote. (GIFTY)
(There’s another way to do that:
git pull origin master but some people don’t recommend this way.)
Merge conflicts are not a systematic problem but a communication problem.
In general, you should keep DRY (Don’t Repeat Yourself). This is the first step for a good programmer from a technical point of view.
However, you have to be careful when you modify your DRY code. Before modifying them, check where that method is used which means the range of effect by modifying. And modify other parts as well so that your app gonna work well.
And later on, I’ll make another post about this — _write a test _!!!
February 11, 2019