如何在Rails 4模型中存储符号?

时间:2022-04-13 07:31:06

I often need to store a constant. Symbols work well for defining constant types, but I don't know how to define them in a Rails model. I would create a separate model to store the types. For example, I have a Game model that has a status attribute.

我经常需要存储一个常数。符号适用于定义常量类型,但我不知道如何在Rails模型中定义它们。我会创建一个单独的模型来存储类型。例如,我有一个具有status属性的Game模型。

class Game < ActiveRecord::Base
  has_one :status
end

The status attribute is an association with another model called Status. The status table has four rows, each defining a different status and each status has a name attribute that's a string.

status属性是与另一个名为Status的模型的关联。状态表有四行,每行定义不同的状态,每个状态都有一个名称属性,即字符串。

def change
  create_table :statuses do |t|
    t.belongs_to :game
    t.timestamps
  end
end

but this feels inefficient and wrong. Ideally, I could have a column in Game table that stores a symbol/constant. Is that possible?

但这感觉效率低下而且错误。理想情况下,我可以在Game表中有一个存储符号/常量的列。那可能吗?

2 个解决方案

#1


2  

Your game status can be thought of as an enumerated type. I like very much storing these as records in a separate database table. It makes them easy to rename and migrate while maintaining data integrity.

您的游戏状态可以被视为枚举类型。我非常喜欢将这些记录存储在单独的数据库表中。它使重命名和迁移变得容易,同时保持数据完整性。

You may also want to try the "enum" feature which is new to Rails 4.1. You could simply declare something like the following

您可能还想尝试Rails 4.1新增的“枚举”功能。您可以简单地声明如下所示的内容

class Game
  enum :status => %w[Init Pause Play End]
end

Note that you would need to add a "status" column of type integer to support this.

请注意,您需要添加一个integer类型的“status”列来支持此操作。

For more try reading http://robots.thoughtbot.com/whats-new-in-edge-rails-active-record-enum http://richonrails.com/articles/active-record-enums-in-ruby-on-rails-4-1

有关更多信息,请阅读http://robots.thoughtbot.com/whats-new-in-edge-rails-active-record-enum http://richonrails.com/articles/active-record-enums-in-ruby-on -rails-4-1

#2


1  

I would create a set of constants to represent the range of possible statuses. These would be stored in the database as ints. Those ints can be compared against Game::Over for example in the code.

我会创建一组常量来表示可能的状态范围。这些将作为int存储在数据库中。这些整数可以与Game :: Over进行比较,例如在代码中。

class Game
  NotStarted = 0
  InPlay     = 1
  Over       = 2

  ...
end

puts "let's get started!" if Game.find(id).status == Game::NotStarted

#1


2  

Your game status can be thought of as an enumerated type. I like very much storing these as records in a separate database table. It makes them easy to rename and migrate while maintaining data integrity.

您的游戏状态可以被视为枚举类型。我非常喜欢将这些记录存储在单独的数据库表中。它使重命名和迁移变得容易,同时保持数据完整性。

You may also want to try the "enum" feature which is new to Rails 4.1. You could simply declare something like the following

您可能还想尝试Rails 4.1新增的“枚举”功能。您可以简单地声明如下所示的内容

class Game
  enum :status => %w[Init Pause Play End]
end

Note that you would need to add a "status" column of type integer to support this.

请注意,您需要添加一个integer类型的“status”列来支持此操作。

For more try reading http://robots.thoughtbot.com/whats-new-in-edge-rails-active-record-enum http://richonrails.com/articles/active-record-enums-in-ruby-on-rails-4-1

有关更多信息,请阅读http://robots.thoughtbot.com/whats-new-in-edge-rails-active-record-enum http://richonrails.com/articles/active-record-enums-in-ruby-on -rails-4-1

#2


1  

I would create a set of constants to represent the range of possible statuses. These would be stored in the database as ints. Those ints can be compared against Game::Over for example in the code.

我会创建一组常量来表示可能的状态范围。这些将作为int存储在数据库中。这些整数可以与Game :: Over进行比较,例如在代码中。

class Game
  NotStarted = 0
  InPlay     = 1
  Over       = 2

  ...
end

puts "let's get started!" if Game.find(id).status == Game::NotStarted