PageSourceSearch

https://shakacode.com/component---src-template-blog-article-tsx-co…m-methods-index-mdx-2f7fe219a1effc3b23a2.js

js shakacode.com collected 2026-10-04 00:29:08 UTC 21,728 bytes, 2 lines download raw bytes

1"use strict";(self.webpackChunkgatsby_starter_default=self.webpackChunkgatsby_starter_default||[]).push([[3502],{36516:function(n,e,a){a.r(e),a.d(e,{Head:function(){return f},default:function(){return b}});var s=a(28453),t=a(96540);function o(n){const e=Object.assign({p:"p",span:"span",a:"a",h2:"h2",h3:"h3",strong:"strong"},(0,s.R)(),n.components);return t.createElement(t.Fragment,null,t.createElement(e.p,null,t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<code class="language-text">ActiveRecord::Enum</code>'}})," is a feature introduced in Rails 4.1\nthat allows you to define attributes with a limited set of allowed values.\nThese values are stored as integers in the database\nbut can be accessed\nand\nmanipulated by name within your Rails application."),"\n",t.createElement(e.p,null,"To know more about Enum functionality,\nplease refer to our previous\n",t.createElement(e.a,{href:"https://www.shakacode.com/blog/enums-and-queries-in-rails-4-dot-1"},"blog post"),"."),"\n",t.createElement(e.h2,{id:"before-rails-71",style:{position:"relative"}},t.createElement(e.a,{href:"#before-rails-71","aria-label":"before rails 71 permalink",className:"anchor before"},t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"Before Rails 7.1"),"\n",t.createElement(e.p,null,"Defining an ",t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<code class="language-text">enum</code>'}})," in ActiveRecord automatically generates\nseveral methods for querying,\nmanipulating,\nand\naccessing the enum values of a model instance."),"\n",t.createElement(e.p,null,"Here's an example of how to define\nand\nuse an enum for an Order ",t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<code class="language-text">status</code>'}})," attribute:"),"\n",t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<div class="gatsby-highlight" data-language="ruby"><pre class="language-ruby"><code class="language-ruby"><span class="token keyword">class</span> <span class="token class-name">Order</span> <span class="token operator">&lt;</span> ApplicationRecord\n  enum status<span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token symbol">pending</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token symbol">shipped</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token symbol">completed</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span>\n<span class="token keyword">end</span>\n\n<span class="token comment"># Access enum values</span>\nOrder<span class="token punctuation">.</span>statuses\n<span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token symbol">pending</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token symbol">shipped</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token symbol">completed</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span>\n\norder <span class="token operator">=</span> Order<span class="token punctuation">.</span>first\norder<span class="token punctuation">.</span>status\n<span class="token operator">=></span> <span class="token string-literal"><span class="token string">\'pending\'</span></span>\n\n<span class="token comment"># Access integer equivalents</span>\nOrder<span class="token punctuation">.</span>statuses<span class="token punctuation">[</span><span class="token symbol">:pending</span><span class="token punctuation">]</span> \n<span class="token operator">=></span> <span class="token number">0</span>\n\norder<span class="token punctuation">.</span>status_value\n<span class="token operator">=></span> <span class="token number">0</span>\n\n<span class="token comment"># Querying based on enum values</span>\nOrder<span class="token punctuation">.</span>shipped\n\n<span class="token constant">SELECT</span> <span class="token string-literal"><span class="token string">"orders"</span></span><span class="token punctuation">.</span><span class="token operator">*</span> <span class="token constant">FROM</span> <span class="token string-literal"><span class="token string">"orders"</span></span> <span class="token constant">WHERE</span> <span class="token string-literal"><span class="token string">"orders"</span></span><span class="token punctuation">.</span><span class="token string-literal"><span class="token string">"status"</span></span> <span class="token operator">=</span> $<span class="token number">1</span> <span class="token operator">/</span><span class="token operator">*</span> loading <span class="token keyword">
1for</span> pp <span class="token operator">*</span><span class="token operator">/</span> <span class="token constant">LIMIT</span> $<span class="token number">2</span>  <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"status"</span></span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"LIMIT"</span></span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">]</span>\n\n<span class="token comment"># verify order values</span>\norder <span class="token operator">=</span> Order<span class="token punctuation">.</span>first\norder<span class="token punctuation">.</span>pending<span class="token operator">?</span>\n<span class="token operator">=></span> <span class="token boolean">true</span>\n\norder<span class="token punctuation">.</span>shipped<span class="token operator">?</span>\n<span class="token operator">=></span> <span class="token boolean">false</span>\n\n<span class="token comment"># update order status</span>\norder<span class="token punctuation">.</span>shipped<span class="token operator">!</span></code></pre></div>'}}),"\n",t.createElement(e.p,null,"Before Rails 7.1,\nif you want to turn off enums,\nthere are workarounds and alternatives you can use:"),"\n",t.createElement(e.h3,{id:"1-override-generated-methods",style:{position:"relative"}},t.createElement(e.a,{href:"#1-override-generated-methods","aria-label":"1 override generated methods permalink",className:"anchor before"},t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"1. Override generated methods:"),"\n",t.createElement(e.p,null,"You can manually override the generated methods in your model class.\nThis approach requires more coding but gives you fine-grained control over the implementation."),"\n",t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<div class="gatsby-highlight" data-language="ruby"><pre class="language-ruby"><code class="language-ruby"><span class="token keyword">class</span> <span class="token class-name">Order</span> <span class="token operator">&lt;</span> ApplicationRecord\n  enum status<span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token symbol">pending</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token symbol">shipped</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token symbol">completed</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span>\n\n  <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">pending</span></span><span class="token operator">?</span>\n    status <span class="token operator">==</span> <span class="token symbol">:pending</span>\n  <span class="token keyword">end</span>\n\n  <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">shipped</span></span><span class="token operator">!</span>\n    update<span class="token operator">!</span><span class="token punctuation">(</span><span class="token symbol">status</span><span class="token operator">:</span> <span class="token symbol">:shipped</span><span class="token punctuation">)</span>\n  <span class="token keyword">end</span>\n<span class="token keyword">end</span></code></pre></div>'}}),"\n",t.createElement(e.h3,{id:"2-use-scopes-instead-of-methods",style:{position:"relative"}}
1,t.createElement(e.a,{href:"#2-use-scopes-instead-of-methods","aria-label":"2 use scopes instead of methods permalink",className:"anchor before"},t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"2. Use scopes instead of methods:"),"\n",t.createElement(e.p,null,"If you mainly use the generated methods for querying,\nconsider using scopes instead.\nScopes are more concise\nand\nefficient for filtering based on enum values."),"\n",t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<div class="gatsby-highlight" data-language="ruby"><pre class="language-ruby"><code class="language-ruby"><span class="token keyword">class</span> <span class="token class-name">Order</span> <span class="token operator">&lt;</span> ApplicationRecord\n  enum status<span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token symbol">pending</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token symbol">shipped</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token symbol">completed</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span>\n\n  scope <span class="token symbol">:pending</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span> where<span class="token punctuation">(</span><span class="token symbol">status</span><span class="token operator">:</span> <span class="token symbol">:pending</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>\n  scope <span class="token symbol">:shipped</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span> where<span class="token punctuation">(</span><span class="token symbol">status</span><span class="token operator">:</span> <span class="token symbol">:shipped</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>\n<span class="token keyword">end</span></code></pre></div>'}}),"\n",t.createElement(e.h3,{id:"3-use-a-rails-plugin",style:{position:"relative"}},t.createElement(e.a,{href:"#3-use-a-rails-plugin","aria-label":"3 use a rails plugin permalink",className:"anchor before"},t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"3. Use a Rails plugin:"),"\n",t.createElement(e.p,null,"Several plugins like ",t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<code class="language-text">enum_without_methods</code>'}}),"\nand\n",t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<code class="language-text">rails_enum_without_methods</code>'}})," provide an easier way to\nturn off enum method generation."),"\n",t.createElement(e.h3,{id:"4-use-a-custom-enum-implementation",style:{position:"relative"}},t.createElement(e.a,{href:"#4-use-a-custom-enum-implementation","aria-label":"4 use a custom enum implementation permalink",className:"anchor before"},t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"4. Use a custom enum implementation:"),"\n",t.createElement(e.p,null,"If you need advanced control over enum functionality,\nconsider implementing your own enum solution without\nrelying on the ActiveRecord methods."),"\n",t.createElement(e.p,null,"Before Rails 7.1,\nwhen disabling methods,\nyou had to be aware of potential side effects\nand\nensure your co
1de handles the enum values correctly."),"\n",t.createElement(e.h2,{id:"in-rails-71",style:{position:"relative"}},t.createElement(e.a,{href:"#in-rails-71","aria-label":"in rails 71 permalink",className:"anchor before"},t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"In Rails 7.1"),"\n",t.createElement(e.p,null,t.createElement(e.a,{href:"https://github.com/rails/rails/pull/46490"},"Rails 7.1 option to disable all methods that ActiveRecord.enum\ngenerates"),".\nYou need to pass ",t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<code class="language-text">instance_methods: false</code>'}})," when declaring the enum\nin the model."),"\n",t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<div class="gatsby-highlight" data-language="ruby"><pre class="language-ruby"><code class="language-ruby"><span class="token keyword">class</span> <span class="token class-name">Order</span> <span class="token operator">&lt;</span> ApplicationRecord\n  enum status<span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token symbol">pending</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token symbol">shipped</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token symbol">completed</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token symbol">instance_methods</span><span class="token operator">:</span> <span class="token boolean">false</span>\n<span class="token keyword">end</span>\n\norder <span class="token operator">=</span> Order<span class="token punctuation">.</span>first\norder<span class="token punctuation">.</span>pending<span class="token operator">?</span>\n<span class="token operator">=></span> <span class="token command-literal"><span class="token command string">`method_missing\': undefined method `</span></span>pending<span class="token operator">?</span>\' <span class="token keyword">for</span> <span class="token comment">#&lt;Order id: "8de...</span></code></pre></div>'}}),"\n",t.createElement(e.p,null,"This feature is beneficial where the methods generated\nby the enum are conflicting,\nand\nmost of the time,\nnot all methods are required."),"\n",t.createElement(e.h3,{id:"how-disabling-activerecord-enums-is-helpful",style:{position:"relative"}},t.createElement(e.a,{href:"#how-disabling-activerecord-enums-is-helpful","aria-label":"how disabling activerecord enums is helpful permalink",className:"anchor before"},t.createElement(e.span,{dangerouslySetInnerHTML:{__html:'<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>'}})),"How disabling ActiveRecord Enums is helpful"),"\n",t.createElement(e.p,null,"** Improves performance:**\nBy disabling the generation of dynamic methods,\nyou can improve the performance of your application."),"\n",t.createElement(e.p,null,t.createElement(e.strong,null,"Reduces code size:"),"\nDisabling enums can reduce the size of your application code,\nwhich can be beneficial in resource-constrained environments."),"\n",t.createElement(e.p,null,t.createElement(e.strong,null,"Provides more control:"),"\nDisabling enums gives you more control over\nhow you handle the enum values in your application."),"\n",t.createElement(e.p,null,t.createElement(e.strong,null,"Easier integration with external systems:"),"\nBy storing the enum values as integers,\nyou can avoid any potential issues with data conversion\nwhen integrating with external systems."),"\n",t.createElement(e.p,null,"To know more about this feature,\nplease refer to this\n",t.createElement(e.a,{href:"https://github.com/rails/rails/pull/46490"},"PR"),"."))}var p=function(n){void 0===n&&(n={});const{wrapper:e}=Object.assign({},(0,s.R)(),n.components);return e?t.createElement(e,n,t.createElement(o,n)):o(n)},l=a(5588),r=a(65427),c=a(20504),i=a(43397),u=a(61942);const m=n=>n.replace(/\/+$/,""),d=(n,e)=>e?""+n+(e.startsWith("/")?e:"/"+e):n,h=(n,e)=>d(m(n),e),k=(n,e)=>d((n=>"undefined"!=typeof window?window.location.origin:m(n))(n),e),g=(n,e)=>{const a=(0,l.d)(e);return a?h(n,a):(n=>h(n,"/shakacode-og.png"))(n)},y=n=>{let{data:e,location:a,children:s}=n;const{frontmatter:o}=e.mdx,{siteUrl:p}=e.site.siteMetadata,l=k(p,a.pathname);return t.createElement(t.Fragment,null,t.createElement(r.A,null),t.createElement(c.A,{header:t.createElement(u.r,{breadcrumb:o.title}),main:t.createElement(t.Fragment,null,t.createElement(u.c,{body:s,title:o.title,author:o.author,date:o.date,link:l,categories:o.categories,featuredImage:o.featuredImage,showFeaturedImage:o.showFeaturedImage}),t.createElement(i.LN,null))}))};function b(n){return t.createElement(y,n,t.createElement(p,n))}const f=n=>{let{data:e,location:a}=n;const{frontmatter:s,excerpt:o}=e.mdx,{siteMetadata:p}
1=e.site,{siteUrl:l,title:r,description:c}=p,i=((n,e,a)=>n.excerpt||e||a)(s,o,c),u=g(l,s.featuredImage),m=h(l,a.pathname);return t.createElement(t.Fragment,null,t.createElement("title",null,s.title+" | "+r),t.createElement("link",{rel:"canonical",href:m}),t.createElement("meta",{name:"description",content:i}),t.createElement("meta",{property:"og:title",content:s.title}),t.createElement("meta",{property:"og:description",content:i}),t.createElement("meta",{property:"og:type",content:"article"}),t.createElement("meta",{property:"og:url",content:m}),t.createElement("meta",{property:"og:image",content:u}),t.createElement("meta",{name:"image",content:u}),t.createElement("meta",{name:"twitter:card",content:"summary_large_image"}),t.createElement("meta",{name:"twitter:title",content:s.title}),t.createElement("meta",{name:"twitter:description",content:i}),t.createElement("meta",{name:"twitter:image",content:u}))}}}]);
2//# sourceMappingURL=component---src-template-blog-article-tsx-content-file-path-src-posts-rails-7-1-adds-the-option-to-disable-activerecord-enum-methods-index-mdx-2f7fe219a1effc3b23a2.js.map

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.