[Feature][ActiveRecord::Enum] Enum maps values to string in the database without hash.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Feature][ActiveRecord::Enum] Enum maps values to string in the database without hash.

Alberto Almagro
Hello again Rails community!

first of all thanks for your time reading this. I apologize in case this appeared before, I swear I have tried a lot of combinations in the search above, found nothing.

Currently Enum allows us to map the values to integers in the database. The typical use case would be something like:

class CreateOrders < ActiveRecord::Migration[5.2]
 
def change
    create_table
:orders do |t|
      t
.integer :status
   
end
 
end
end


class Order < ApplicationRecord
 
enum status: [:ok, :billing, :failed]
end



This would map the following:
Order.statuses[:ok]       # => 0
Order.statuses[:billing]  # => 1
Order.statuses[:failed]   # => 2


The problem with integers is that they don't read well in the database. Seeing status = 0 isn't meaningful, so at the end lots of teams, including us, end having a string in the database and mapping enums with a hash.


class CreateOrders < ActiveRecord::Migration[5.2]
 
def change
    create_table
:orders do |t|
      t
.string :status
   
end
 
end
end


class Order < ApplicationRecord
 
enum status: { ok: 'ok', billing: 'billing', failed: 'failed' }
end

Writing this really feels to us like unnecessary boilerplate. I would like to write a PR to allow having a string in the database and declaring the enum with an array of symbols.

class CreateOrders < ActiveRecord::Migration[5.2]
 
def change
    create_table
:orders do |t|
      t
.string :status
   
end
 
end
end


class Order < ApplicationRecord
 
enum status: [:ok, :billing, :failed]
end

I think this would feel much better, cleaner. Of course I will keep current options and syntax. Do you like it?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord::Enum] Enum maps values to string in the database without hash.

Kasper Timm Hansen
I’m +1 as I’ve wanted this in the past. I think enum actually did support this
but it was never documented (e.g. private API).

But I remember something about others having objections to supporting
string columns. Perhaps Rafael or Sean?

Den 20. jun. 2018 kl. 22.08 skrev Alberto Almagro <[hidden email]>:

Hello again Rails community!

first of all thanks for your time reading this. I apologize in case this appeared before, I swear I have tried a lot of combinations in the search above, found nothing.

Currently Enum allows us to map the values to integers in the database. The typical use case would be something like:

class CreateOrders < ActiveRecord::Migration[5.2]
 
def change
    create_table
:orders do |t|
      t
.integer :status
   
end
 
end
end


class Order < ApplicationRecord
 
enum status: [:ok, :billing, :failed]
end



This would map the following:
Order.statuses[:ok]       # => 0
Order.statuses[:billing]  # => 1
Order.statuses[:failed]   # => 2


The problem with integers is that they don't read well in the database. Seeing status = 0 isn't meaningful, so at the end lots of teams, including us, end having a string in the database and mapping enums with a hash.


class CreateOrders < ActiveRecord::Migration[5.2]
 
def change
    create_table
:orders do |t|
      t
.string :status
   
end
 
end
end


class Order < ApplicationRecord
 
enum status: { ok: 'ok', billing: 'billing', failed: 'failed' }
end

Writing this really feels to us like unnecessary boilerplate. I would like to write a PR to allow having a string in the database and declaring the enum with an array of symbols.

class CreateOrders < ActiveRecord::Migration[5.2]
 
def change
    create_table
:orders do |t|
      t
.string :status
   
end
 
end
end


class Order < ApplicationRecord
 
enum status: [:ok, :billing, :failed]
end

I think this would feel much better, cleaner. Of course I will keep current options and syntax. Do you like it?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
Kasper

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord::Enum] Enum maps values to string in the database without hash.

Alberto Almagro
Thanks Kasper for your reply! I'm really happy to see that you're +1 on this.

I would like to start implementing it on the weekend, it would be great if someone else shares her/his thoughts about it before.

El jueves, 21 de junio de 2018, 9:15:50 (UTC+2), Kasper Timm Hansen escribió:
I’m +1 as I’ve wanted this in the past. I think enum actually did support this
but it was never documented (e.g. private API).

But I remember something about others having objections to supporting
string columns. Perhaps Rafael or Sean?

Den 20. jun. 2018 kl. 22.08 skrev Alberto Almagro <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="aaKoR6bEAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">alberto...@...>:

Hello again Rails community!

first of all thanks for your time reading this. I apologize in case this appeared before, I swear I have tried a lot of combinations in the search above, found nothing.

Currently <a href="http://api.rubyonrails.org/v5.2/classes/ActiveRecord/Enum.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fapi.rubyonrails.org%2Fv5.2%2Fclasses%2FActiveRecord%2FEnum.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGsJfR3GE5309LxWo5Fog9yWRBKuQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fapi.rubyonrails.org%2Fv5.2%2Fclasses%2FActiveRecord%2FEnum.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGsJfR3GE5309LxWo5Fog9yWRBKuQ&#39;;return true;">Enum allows us to map the values to integers in the database. The typical use case would be something like:

class CreateOrders < ActiveRecord::Migration[5.2]
 
def change
    create_table
:orders do |t|
      t
.integer :status
   
end
 
end
end


class Order < ApplicationRecord
 
enum status: [:ok, :billing, :failed]
end



This would map the following:
Order.statuses[:ok]       # => 0
Order.statuses[:billing]  # => 1
Order.statuses[:failed]   # => 2


The problem with integers is that they don't read well in the database. Seeing status = 0 isn't meaningful, so at the end lots of teams, including us, end having a string in the database and mapping enums with a hash.


class CreateOrders < ActiveRecord::Migration[5.2]
 
def change
    create_table
:orders do |t|
      t
.string :status
   
end
 
end
end


class Order < ApplicationRecord
 
enum status: { ok: 'ok', billing: 'billing', failed: 'failed' }
end

Writing this really feels to us like unnecessary boilerplate. I would like to write a PR to allow having a string in the database and declaring the enum with an array of symbols.

class CreateOrders < ActiveRecord::Migration[5.2]
 
def change
    create_table
:orders do |t|
      t
.string :status
   
end
 
end
end


class Order < ApplicationRecord
 
enum status: [:ok, :billing, :failed]
end

I think this would feel much better, cleaner. Of course I will keep current options and syntax. Do you like it?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="aaKoR6bEAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-co...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="aaKoR6bEAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;">https://groups.google.com/group/rubyonrails-core.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
Kasper

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.