多列的枚举值相同

时间:2021-10-03 10:03:39

I need to do something like this:

我需要做这样的事情:

class PlanetEdge < ActiveRecord::Base
  enum :first_planet [ :earth, :mars, :jupiter]
  enum :second_planet [ :earth, :mars, :jupiter]
end

Where my table is a table of edges but each vertex is an integer.

我的表是一个边的表,但每个顶点是一个整数。

However, it seems the abvove is not possible in rails. What might be an alternative to making string columns?

然而,似乎在铁轨中不可能出现这种情况。什么可能是制作字符串列的替代方法?

2 个解决方案

#1


2  

Maybe it makes extract the planet as another model?

也许它让这个星球成为另一个模型?

class Planet < ActiveRecord::Base
  enum type: %w(earth mars jupiter)
end

class PlanetEdge < ActiveRecord::Base
  belongs_to :first_planet, class_name: 'Planet'
  belongs_to :second_planet, class_name: 'Planet'
end

You can create a PlanetEdge by using accepts_nested_attributes_for:

您可以使用accepts_nested_attributes_for创建PlanetEdge:

class PlanetEdge < ActiveRecord::Base
  belongs_to :first_planet, class_name: 'Planet'
  belongs_to :second_planet, class_name: 'Planet'

  accepts_nested_attributes_for :first_planet
  accepts_nested_attributes_for :second_planet
end

PlanetEdge.create(
  first_planet_attributes: { type: 'mars' },
  second_planet_attributes: { type: 'jupiter' }
)

#2


5  

If you're running rails 5.0 or greater as per http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

如果您按照http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html运行rails 5.0或更高版本

You can use the :_prefix or :_suffix options when you need to define multiple enums with same values. If the passed value is true, the methods are prefixed/suffixed with the name of the enum. It is also possible to supply a custom value:

当您需要定义具有相同值的多个枚举时,可以使用:_prefix或:_suffix选项。如果传递的值为true,则方法的前缀/后缀为枚举的名称。也可以提供自定义值:

class Conversation < ActiveRecord::Base
    enum status: [:active, :archived], _suffix: true
    enum comments_status: [:active, :inactive], _prefix: :comments
end

With the above example, the bang and predicate methods along with the associated scopes are now prefixed and/or suffixed accordingly:

在上面的例子中,bang和谓词方法以及相关的范围现在都是相应的前缀和/或后缀:

conversation.active_status!
conversation.archived_status? # => false

conversation.comments_inactive!
conversation.comments_active? # => false

#1


2  

Maybe it makes extract the planet as another model?

也许它让这个星球成为另一个模型?

class Planet < ActiveRecord::Base
  enum type: %w(earth mars jupiter)
end

class PlanetEdge < ActiveRecord::Base
  belongs_to :first_planet, class_name: 'Planet'
  belongs_to :second_planet, class_name: 'Planet'
end

You can create a PlanetEdge by using accepts_nested_attributes_for:

您可以使用accepts_nested_attributes_for创建PlanetEdge:

class PlanetEdge < ActiveRecord::Base
  belongs_to :first_planet, class_name: 'Planet'
  belongs_to :second_planet, class_name: 'Planet'

  accepts_nested_attributes_for :first_planet
  accepts_nested_attributes_for :second_planet
end

PlanetEdge.create(
  first_planet_attributes: { type: 'mars' },
  second_planet_attributes: { type: 'jupiter' }
)

#2


5  

If you're running rails 5.0 or greater as per http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

如果您按照http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html运行rails 5.0或更高版本

You can use the :_prefix or :_suffix options when you need to define multiple enums with same values. If the passed value is true, the methods are prefixed/suffixed with the name of the enum. It is also possible to supply a custom value:

当您需要定义具有相同值的多个枚举时,可以使用:_prefix或:_suffix选项。如果传递的值为true,则方法的前缀/后缀为枚举的名称。也可以提供自定义值:

class Conversation < ActiveRecord::Base
    enum status: [:active, :archived], _suffix: true
    enum comments_status: [:active, :inactive], _prefix: :comments
end

With the above example, the bang and predicate methods along with the associated scopes are now prefixed and/or suffixed accordingly:

在上面的例子中,bang和谓词方法以及相关的范围现在都是相应的前缀和/或后缀:

conversation.active_status!
conversation.archived_status? # => false

conversation.comments_inactive!
conversation.comments_active? # => false