{"id":15189,"date":"2021-02-22T17:53:12","date_gmt":"2021-02-22T12:23:12","guid":{"rendered":"https:\/\/www.botreetechnologies.com\/blog\/?p=15189"},"modified":"2021-04-20T12:10:13","modified_gmt":"2021-04-20T06:40:13","slug":"rails-define-method-in-models","status":"publish","type":"post","link":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/","title":{"rendered":"Rails: define_method in Models"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>define_method<\/strong> is a method that allows you to create other methods.<\/h2>\n\n\n\n<p>The usual method for defining the method in <a href=\"https:\/\/www.botreetechnologies.com\/\">Rails app<\/a> is to put your logic between the def and end block.<\/p>\n\n\n\n<p>Now, there\u2019s a situation where you have to build a series of methods and all methods have the same logic and structures to get the batch status.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s go through the example for better understanding.<\/p>\n\n\n\n<p>There are scenarios where you need to check if your batch status is <strong>drafted or emailed or approved.<\/strong><\/p>\n\n\n\n<p>Now, you might say that we will just define one method and accept those statuses as a parameter, and we can use it whenever we want or require.<\/p>\n\n\n\n<p>Yes, you are right!&nbsp;<\/p>\n\n\n\n<p><strong><em>Example Code:<\/em><\/strong><\/p>\n\n\n<p><script src=\"https:\/\/gist.github.com\/Chandni-BTC\/68c057cd09358ba2c110787ae3a6b3d4.js\"><\/script><\/p>\n\n\n<p>We have these three methods and all these methods have different names and the same logic. Each method returns the boolean value.<\/p>\n\n\n\n<p>For the above situation, we have a powerful entity named <strong>define_method<\/strong>, which will dynamically create methods and will reduce the amount of code.<\/p>\n\n\n\n<p>In our example for 3 different parameters, we can create one object which returns 3 different queries and different method names for those 3 different parameters, which means define_method is provided to the object\u2019s function.<\/p>\n\n\n\n<p>We can use define_method in <a href=\"https:\/\/www.botreetechnologies.com\/ruby-on-rails-development\">Ruby on Rails<\/a> to make one method for the above 3 methods.<\/p>\n\n\n\n<p>Do you want to check this?&nbsp;<\/p>\n\n\n\n<p><strong><em>Let\u2019s check the below code.<\/em><\/strong><\/p>\n\n\n<p><script src=\"https:\/\/gist.github.com\/Chandni-BTC\/b54a6ff3296fcdc947c2dbaba5e53800.js\"><\/script><\/p>\n\n\n<h2 class=\"wp-block-heading\">Let&#8217;s start from the first, adding methods based on a series of states:<\/h2>\n\n\n\n<p>Here&#8217;s an example where we call it inside a loop. It achieves the same result as above.<\/p>\n\n\n<p><script src=\"https:\/\/gist.github.com\/Chandni-BTC\/f93387da1e21fa165437a3e8f01a86c2.js\"><\/script><\/p>\n\n\n<p><strong><em>We can use these methods like:<\/em><\/strong><\/p>\n\n\n<p><script src=\"https:\/\/gist.github.com\/Chandni-BTC\/a19d061a5176f441b4e277ce1988291e.js\"><\/script><\/p>\n<p>How do you pass arguments to define_method?<br><strong>Example Code:<\/strong><\/p>\n<p><script src=\"https:\/\/gist.github.com\/Chandni-BTC\/06a062ebb9b95bcfb269a23620c02f08.js\"><\/script><\/p>\n\n\n<p><strong><em>We can use this method like:&nbsp;<\/em><\/strong><\/p>\n\n\n<p><script src=\"https:\/\/gist.github.com\/Chandni-BTC\/193831dae57a2e97d7c618b611d8a3ee.js\"><\/script><\/p>\n\n\n<p>In fact, we can now extend this method by adding any number of elements to that given array or we can replace the array entity with another data.<\/p>\n\n\n\n<p>Hope this will be helpful to you to reduce the code and improve your code quality. Thanks for reading!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.botreetechnologies.com\/contact\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"150\" src=\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/12\/call-now-ruby-on-rails-web-development.png\" alt=\"call now ruby on rails web development\" class=\"wp-image-13325\" srcset=\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/12\/call-now-ruby-on-rails-web-development.png 700w, https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/12\/call-now-ruby-on-rails-web-development-300x64.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/figure>\n\n\n\n<p><br><strong><br><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>define_method is a method that allows you to create other&#8230;<\/p>\n","protected":false},"author":66,"featured_media":15197,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,10],"tags":[],"class_list":["post-15189","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails","category-technology"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Rails: define_method in Models<\/title>\n<meta name=\"description\" content=\"Rails:define_method in models can enable you to reduce the code you have to write while improving the code quality. Read this to get a better understanding.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Rails: define_method in Models\" \/>\n<meta property=\"og:description\" content=\"Rails:define_method in models can enable you to reduce the code you have to write while improving the code quality. Read this to get a better understanding.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/\" \/>\n<meta property=\"og:site_name\" content=\"BoTree Technologies\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BoTreeTechnologies\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-22T12:23:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-20T06:40:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2021\/02\/rails-define-method-in-models.png\" \/>\n\t<meta property=\"og:image:width\" content=\"852\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Chandni Thakkar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@BoTreeTech\" \/>\n<meta name=\"twitter:site\" content=\"@BoTreeTech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Chandni Thakkar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/\"},\"author\":{\"name\":\"Chandni Thakkar\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/b0d4e4ab663785d358769d287d034b48\"},\"headline\":\"Rails: define_method in Models\",\"datePublished\":\"2021-02-22T12:23:12+00:00\",\"dateModified\":\"2021-04-20T06:40:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/\"},\"wordCount\":341,\"image\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2021\/02\/rails-define-method-in-models.png\",\"articleSection\":[\"Ruby on Rails\",\"Technology\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/\",\"name\":\"Rails: define_method in Models\",\"isPartOf\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2021\/02\/rails-define-method-in-models.png\",\"datePublished\":\"2021-02-22T12:23:12+00:00\",\"dateModified\":\"2021-04-20T06:40:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/b0d4e4ab663785d358769d287d034b48\"},\"description\":\"Rails:define_method in models can enable you to reduce the code you have to write while improving the code quality. Read this to get a better understanding.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#primaryimage\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2021\/02\/rails-define-method-in-models.png\",\"contentUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2021\/02\/rails-define-method-in-models.png\",\"width\":852,\"height\":420,\"caption\":\"Rails: define_method in Models\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.botreetechnologies.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Rails: define_method in Models\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#website\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/\",\"name\":\"BoTree Technologies\",\"description\":\"Committed to inspire generation.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.botreetechnologies.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/b0d4e4ab663785d358769d287d034b48\",\"name\":\"Chandni Thakkar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/0165caa9d669d60194be4be1ad47cbc82196dc3e0f40740b2ae7731e3de3eff2?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0165caa9d669d60194be4be1ad47cbc82196dc3e0f40740b2ae7731e3de3eff2?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0165caa9d669d60194be4be1ad47cbc82196dc3e0f40740b2ae7731e3de3eff2?s=96&d=mm&r=g\",\"caption\":\"Chandni Thakkar\"},\"description\":\"I am a Ruby On Rails Developer. I like to debug, logical problem-solving in Ruby, a quick learner, blog writing. Apart from coding, I also like to travel and listen to music.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Rails: define_method in Models","description":"Rails:define_method in models can enable you to reduce the code you have to write while improving the code quality. Read this to get a better understanding.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/","og_locale":"en_US","og_type":"article","og_title":"Rails: define_method in Models","og_description":"Rails:define_method in models can enable you to reduce the code you have to write while improving the code quality. Read this to get a better understanding.","og_url":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/","og_site_name":"BoTree Technologies","article_publisher":"https:\/\/www.facebook.com\/BoTreeTechnologies\/","article_published_time":"2021-02-22T12:23:12+00:00","article_modified_time":"2021-04-20T06:40:13+00:00","og_image":[{"width":852,"height":420,"url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2021\/02\/rails-define-method-in-models.png","type":"image\/png"}],"author":"Chandni Thakkar","twitter_card":"summary_large_image","twitter_creator":"@BoTreeTech","twitter_site":"@BoTreeTech","twitter_misc":{"Written by":"Chandni Thakkar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#article","isPartOf":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/"},"author":{"name":"Chandni Thakkar","@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/b0d4e4ab663785d358769d287d034b48"},"headline":"Rails: define_method in Models","datePublished":"2021-02-22T12:23:12+00:00","dateModified":"2021-04-20T06:40:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/"},"wordCount":341,"image":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#primaryimage"},"thumbnailUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2021\/02\/rails-define-method-in-models.png","articleSection":["Ruby on Rails","Technology"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/","url":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/","name":"Rails: define_method in Models","isPartOf":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#primaryimage"},"image":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#primaryimage"},"thumbnailUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2021\/02\/rails-define-method-in-models.png","datePublished":"2021-02-22T12:23:12+00:00","dateModified":"2021-04-20T06:40:13+00:00","author":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/b0d4e4ab663785d358769d287d034b48"},"description":"Rails:define_method in models can enable you to reduce the code you have to write while improving the code quality. Read this to get a better understanding.","breadcrumb":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#primaryimage","url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2021\/02\/rails-define-method-in-models.png","contentUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2021\/02\/rails-define-method-in-models.png","width":852,"height":420,"caption":"Rails: define_method in Models"},{"@type":"BreadcrumbList","@id":"https:\/\/www.botreetechnologies.com\/blog\/rails-define-method-in-models\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.botreetechnologies.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Rails: define_method in Models"}]},{"@type":"WebSite","@id":"https:\/\/www.botreetechnologies.com\/blog\/#website","url":"https:\/\/www.botreetechnologies.com\/blog\/","name":"BoTree Technologies","description":"Committed to inspire generation.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.botreetechnologies.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/b0d4e4ab663785d358769d287d034b48","name":"Chandni Thakkar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/0165caa9d669d60194be4be1ad47cbc82196dc3e0f40740b2ae7731e3de3eff2?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/0165caa9d669d60194be4be1ad47cbc82196dc3e0f40740b2ae7731e3de3eff2?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0165caa9d669d60194be4be1ad47cbc82196dc3e0f40740b2ae7731e3de3eff2?s=96&d=mm&r=g","caption":"Chandni Thakkar"},"description":"I am a Ruby On Rails Developer. I like to debug, logical problem-solving in Ruby, a quick learner, blog writing. Apart from coding, I also like to travel and listen to music."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/15189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/users\/66"}],"replies":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=15189"}],"version-history":[{"count":2,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/15189\/revisions"}],"predecessor-version":[{"id":15201,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/15189\/revisions\/15201"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/media\/15197"}],"wp:attachment":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=15189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=15189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=15189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}