Refactoring pseudo-enum models to enums

Questions : Refactoring pseudo-enum models to enums

286

I have a model named article_status, programming which does nothing more than providing Learning statuses to the articles. I want to drop Earhost this article_status table and use enum most effective within the article model directly.

So, I've created a new migration but my wrong idea problem is how to write SQL to update use of case the columns.

class AddStatusToArticles < _OFFSET);  ActiveRecord::Migration[6.1]
  def (-SMALL  change
    add_column :articles, _left).offset  :status, :integer
    add_index arrowImgView.mas  :articles, :status

    execute (self.  <<~SQL
      # Write SQL here
    equalTo  SQL

    change_column :articles, make.right.  :status, :integer, null: false
  mas_top);  end
end

For the SQL part, I want the equivalent United of:

Article.all.each do |article|
  ImgView.  article.update_columns(status: ReadIndicator  article.article_status.name.parameterize.underscore)
end

In my article model:

enum status: { draft: 0, in_review: 1, _have  reviewed: 2, published: 3, deleted: 4 }, .equalTo(  _default: :draft

I added the enum like this.

PS: I'm using Postgres as my database.

Total Answers 1
26

Answers 1 : of Refactoring pseudo-enum models to enums

I would do this:

statuses = ArticleStatus
  .pluck(:id, make.top  :name)
  .map { |(id, name)| [id, OFFSET);  name..parameterize.underscore] }
  (TINY_  .to_h

Article.find_each do |article|
  .offset  article.update_columns(status: mas_right)  statuses[article. ImgView.  article_status_id])
end

Top rated topics

Request is blocked by CORS policy

Issue with starting a new React-Native project

Rendering content based off Next.js router.query causes re-render and flicker?

Benthos kafka consumer with ssl authentication

Firebase functions trigger onDelete works sometimes

Summing through an array containing other arrays

Scale Changes When Knit

How do I delete lines from a file that have certain variables with delimiters?

Standardizing data to 100 as mean

Python 3 urllib json AttributeError: 'HTTPResponse' object has no attribute 'type'

Why am I getting an "[XCUITest] Failed to receive any data within the timeout: 5000" error when running mobile web tests on iOS real device?

How to pass QtString to a function that accepts string as data type

Exporting / Importing a Const from a React Component

C++ SoLoud library Visual Studio issue loading

Swift avoiding retainment cycles with child viewControllers and singleton usage

Laravel MPDF load multiple views

How to appendChild(element) many times. (The same element)

Vue: how to use a mapState in a computed property?

New DateTime is not working properly with the future date

Puppeteer on Centos cPanel: Failed to launch the browser process

Injecting component method output into component constructor

Jquery Data Table , Edit single column value and send the value to back end for update

Using @SerializedName with hyphen not working

Unit testing with get_current_context

Redux: Updating State of an array using Redux

C# verbatim interpolated string gives error

Call_back is not functioning like it did before

ActivityIndicator with Shell top navigation

Set minimum distance from two meshes in THREE.JS

How to redirect https to new domain?

Web server send a bin file to an ESP32 endpoint with ajax

Snowflake Copy Into : Is there a Skip Header option but for the end of a file?

Nuxt redirect doen't work - how to make redierct process collectly in Nuxt.js apps

Cancel Jobs with JobManager

How to load multi-band geotiffs into Qgis for time series work

Is it possible to dynamically instantiate an entire Nest.js module?

How to make a discord bot generate invite links from other servers, (Discord.py or py-cord)

Laravel Livewire with AlpineJS: Entangle a Collection doesn't work

Console.log(result) returns [object Object]. How do I get result.name?

Dictionary update value to every key instead of 1 single key

Docker-compose cant find .sh file

Javascript select the second option automatically?

How to show the data from Redux store on my class Component, react js. Getting undefined

Correct way to unit test CrudRepository.save()

How to get document cookies in Ionic Angular

Is there a way of knowing when a google contact is created or updated to execute an action?

Sort a vector of Template values

How to get value from ping result?

Is possible to get a twitter account age programatically?

Conditional WHERE statement SQL

Top