{"id":102654,"date":"2020-10-26T14:34:01","date_gmt":"2020-10-26T11:34:01","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid\/"},"modified":"2020-10-26T14:34:01","modified_gmt":"2020-10-26T11:34:01","slug":"the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid\/","title":{"rendered":"#The 7 Most Common Ruby on Rails Programming Mistakes To Avoid"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2d921758b7f\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2d921758b7f\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid\/#The_7_Most_Common_Mistakes_in_Ruby_On_Rails_Development\" >The 7 Most Common Mistakes in Ruby On Rails Development<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid\/#Common_Mistake_1_Do_You_Check_N_1_Query_Rail\" >Common Mistake #1: Do You Check N + 1 Query Rail<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid\/#Common_Mistake_2_Not_using_memoization\" >Common Mistake #2: Not using memoization:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid\/#Common_Mistake_3_Improper_Predicate_Method_Usage\" >Common Mistake #3: Improper Predicate Method Usage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid\/#Common_Mistake_4_Avoid_Blocking_on_Calls\" >Common Mistake #4: Avoid Blocking on Calls\u00a0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid\/#Common_Mistake_5_Lack_of_automated_tests\" >Common Mistake #5: Lack of automated tests<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid\/#Common_Mistake_6_Follow_Proper_Naming_Convention\" >Common Mistake #6: Follow Proper Naming Convention<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid\/#Common_Mistake_7_Keep_Your_Configurations_Safe\" >Common Mistake #7: Keep Your Configurations Safe<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid\/#Final_Thoughts\" >Final Thoughts<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<p>&#8220;<strong>#The 7 Most Common Ruby on Rails Programming Mistakes To Avoid<\/strong>&#8221;<\/p>\n<div class=\"entry-inner\">\n<p class=\"opener\">Ruby on Rails is one of the most popular and widely used web development frameworks. It is a relatively easy and powerful web development framework for building scalable and enterprise-ready <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lications.<\/p>\n<p>It heavily emphasizes convention over configuration and allows new Rails applications to be running in a fraction of time. In this blog, we will list those common mistakes in Rails development and see how to avoid them. So without further ado, let\u2019s get started!<\/p>\n<hr class=\"wp-block-separator\">Most Ruby developers love the RubyGems that can reduce the development time and write excess code. Ruby\u2019s best and easiest thing on Rails development like MVC pattern for highly-functional apps and convention over configuration makes it reliable and scalable. There are some pitfalls of Ruby on Rails development that can be overcome by experience.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"The_7_Most_Common_Mistakes_in_Ruby_On_Rails_Development\"><\/span><strong>The 7 Most Common Mistakes in Ruby On Rails Development<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Common_Mistake_1_Do_You_Check_N_1_Query_Rail\"><\/span><strong>Common Mistake #1: Do You Check N + 1 Query Rail<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If you\u2019re checking the N + 1 Query, it <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>ly won\u2019t address any issue in your application. Still, sometimes it may affect your application and slow down the performance, which is the most common mistake by Ruby developers.<\/p>\n<p><strong>Eg<\/strong>:<\/p>\n<pre class=\"wp-block-preformatted\">users = User.where(is_active:true)<br>names = users.map { |user| user.profile.name }<\/pre>\n<p>Avoid N + 1 query when querying associated records.<\/p>\n<p><strong>Eg:<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">users = User.where(is_active:true).includes(:profile)<br>names = users.map { |user| user.profile.name }<\/pre>\n<p>You can use a bullet gem to improve your app\u2019s performance by reducing the number of queries and adding eager loading where required.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Common_Mistake_2_Not_using_memoization\"><\/span><strong>Common Mistake #2: Not using memoization:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Memoization is a process that is used to improve your assessor\u2019s speed during Ruby on Rails development by caching the results of methods that are time-consuming or variables initializing.<\/p>\n<p>Memoization uses the ||= operator for some value in parameters and then initializes the cache variables.<\/p>\n<p><strong>Eg:<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">Def google_calendar_event\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0@event||=GoogleCalendarEvent.fing_by(event_id:params[:event_id]}\n\nEnd<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Common_Mistake_3_Improper_Predicate_Method_Usage\"><\/span><strong>Common Mistake #3: Improper Predicate Method Usage<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Using an improper predicate method is another common mistake done during Rails development. You should be aware of the predicate method, which ends with a question mark and returns a boolean value(true\/false). But it is important to understand that the slight difference between the potential values in Ruby development.<\/p>\n<p>When you\u2019re creating your own predicate methods, understand their purpose before altering any data. For instance, the predicate method determines if your favorite book is currently available in the library or not; if not, it will change the book. library property to true:<\/p>\n<p><strong>Eg:<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">def favorite_book_in_library?\n\u00a0\u00a0book = favorite_book\n\u00a0\u00a0unless book.library\n\u00a0\u00a0\u00a0\u00a0# If not, add a library.\n\u00a0\u00a0\u00a0\u00a0book.library = true\n\u00a0\u00a0\u00a0\u00a0true\n\u00a0\u00a0end\nend<\/pre>\n<p>To examine your existing data that returns a boolean value using the predicate method, avoid using direct data manipulation. Because in such cases, you\u2019ll have to remove the assignment value of the property check with a single line that is:<\/p>\n<pre class=\"wp-block-preformatted\"># Check if your favorite book is in the library.\ndef favorite_book_in_library?\n\u00a0\u00a0return favorite_book.library\nend<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Common_Mistake_4_Avoid_Blocking_on_Calls\"><\/span><strong>Common Mistake #4: Avoid Blocking on Calls\u00a0<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>It is easy to integrate APIs using the 3rd party providers of Rails applications, but sometimes you may run slow. To avoid blocking on calls, move some part of code to the background job, instead of calling services from your Rails application.<\/p>\n<p>Delayed Job and Sidekiq are the two most popular Rubygems used for application development for this purpose.<\/p>\n<p>For instance, when a new user creates an account and provides the phone number, your application may use a third-party service to authenticate the multi-factor confirmation code.<\/p>\n<p>Here is the code snippet when you\u2019re using the send_authentication_code method:<\/p>\n<pre class=\"wp-block-preformatted\">def send_authentication_code(number, code)\n\u00a0\u00a0account_sid = \"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\" # Your Account SID from www.twilio.com\/console\n\u00a0\u00a0auth_token = \"your_auth_token\" \u00a0 # Your Auth Token from www.twilio.com\/console\n\n\u00a0\u00a0@client = Twilio::REST::Client.new account_sid, auth_token\n\u00a0\u00a0message = @client.messages.create(\n\u00a0\u00a0\u00a0\u00a0body: code,\n\u00a0\u00a0\u00a0\u00a0to: number,\n\u00a0\u00a0\u00a0\u00a0from: ENV['TWILIO_NUMBER'])\n\n\u00a0\u00a0puts message.sid\nend<\/pre>\n<p style=\"text-align:center\"><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/airbrake.io\/blog\/rails\/5-common-mistakes-rails-development\">Code<\/a><\/p>\n<hr class=\"wp-block-separator\">If you call <em>send_authentication_code <\/em>in your application, you risk your application when the third-party service request is executed.<\/p>\n<p>To avoid such risk, integrate a job system that can store and implement work BTS in your main application threads.<\/p>\n<p>You may also like: <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/www.bacancytechnology.com\/blog\/common-mistakes-in-ruby-on-rails-development\">Most Common Mistakes in Ruby on Rails Development (And How to Avoid Making Them)<\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Common_Mistake_5_Lack_of_automated_tests\"><\/span><strong>Common Mistake #5: Lack of automated tests<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Ruby on Rails is a powerful web application framework with automated test capabilities by default. Rails developers write practical tests using different styles, such as BDD and TDD, to make it even more powerful test frameworks.<\/p>\n<p>There are various debates on how comprehensive your testing should be, and it shows the importance of automated testing for every application.<\/p>\n<p>Your application must consist of at least one high-level integration test written for your controllers to edit and modify the code of your Ruby on Rails version with a clear delineation of the Ruby application\u2019s full collection of functionality.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Common_Mistake_6_Follow_Proper_Naming_Convention\"><\/span><strong>Common Mistake #6: Follow Proper Naming Convention<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>While developing Rails projects, keep a few points in your mind to avoid future issues:<\/p>\n<ul>\n<li>The model name should be in the singular.<\/li>\n<li>To map your model and table automatically, make sure that all the available tables are in a plural format.<\/li>\n<li>Do not reserve names for your class.<\/li>\n<li>Always follow the default restful routes to avoid the complexity.<\/li>\n<\/ul>\n<p>Ruby on Rails is a feature-rich web application framework with some limitations and common mistakes that should be avoided while developing an application.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Common_Mistake_7_Keep_Your_Configurations_Safe\"><\/span><strong>Common Mistake #7: Keep Your Configurations Safe<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>While developing Rails applications, your application might be using external services such as Google Calendar, AWS, and its API keys stored in credentials. yml.<\/p>\n<p>The repository files may be checked into the source code using the rest of your application when accessing the storage becomes easy for all users of your application.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Final_Thoughts\"><\/span><strong>Final Thoughts<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Rail is a powerful framework for building web applications that hide many ugly details necessary to develop a robust web application. To make your development process faster, you should pay attention to the potential design and coding errors that make your application easily maintainable and extensible as they grow.<\/p>\n<p>You should be aware of such issues that can make your application less secure, less reliable, and reduce application performance. Study the full framework architecture and make sure that you\u2019re aware of the coding tradeoffs throughout the development process and then start building your Ruby on Rails project.<\/p>\n<p>If you avoid such common mistakes, you can develop top-notch and high-quality Ruby on Rails web applications. If you\u2019re planning to develop such web applications, Hire Ruby on Rails Developers who have already learned from these mistakes to avoid halting your project\u2019s development and have error-free development.<\/p>\n<hr class=\"wp-block-separator\"><em>Photo by <\/em><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/unsplash.com\/@emilep?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\"><em>Emile Perron<\/em><\/a><em> on <\/em><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/unsplash.com\/s\/photos\/programming?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\"><em>Unsplash<\/em><\/a>\n<\/div>\n<p><\/p>\n<div class=\"author-inner\">\n<p class=\"bio-name\">Chandresh Patel<\/p>\n<div class=\"bio-desc\">\nChandresh Patel is a CEO, Agile coach and founder of Bacancy <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/technology\/\" data-internallinksmanager029f6b8e52c=\"4\" title=\"Technology\" target=\"_blank\" rel=\"noopener\">Technology<\/a>. His truly entrepreneurial spirit, skillful expertise and extensive knowledge in the Agile software development services have helped the organization to achieve new heights of success. Chandresh is fronting the organization into global markets in a systematic, innovative and collaborative way to fulfill custom software development needs and provide optimum quality services.<\/div>\n<p><!-- social-link --><\/p>\n<div class=\"clear\"><\/div>\n<\/div>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/news\/\" data-internallinksmanager029f6b8e52c=\"2\" title=\"News\" target=\"_blank\" rel=\"noopener\">News<\/a> articles, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/general\/\" target=\"_blank\" rel=\"noopener noreferrer\">General <\/a><\/span>category.<\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.noupe.com\/development\/the-7-most-common-ruby-on-rails-programming-mistakes-to-avoid.html\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#The 7 Most Common Ruby on Rails Programming Mistakes To Avoid&#8221; Ruby on Rails is one of the most popular and widely used web development frameworks. It is a relatively easy and powerful web development framework for building scalable and enterprise-ready applications. It heavily emphasizes convention over configuration and allows new Rails applications to be&#8230;<\/p>\n","protected":false},"author":1,"featured_media":102655,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.noupe.com\/wp-content\/uploads\/2020\/10\/emile-perron-xrVDYZRGdw4-unsplash.jpg","fifu_image_alt":"","footnotes":""},"categories":[1],"tags":[75083,73821,78076],"class_list":["post-102654","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","tag-developer","tag-developers","tag-development-tools"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/102654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=102654"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/102654\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/102655"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=102654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=102654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=102654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}