简体   繁体   English

我应该在rails 3中使用boolean fields还是state_machine gem?

[英]Should I use boolean fields or state_machine gem in rails 3?

I have a e-mail system and I want that an user can decide with checkboxes what message want receive when happen determinate events, eg 我有一个电子邮件系统,我希望用户可以通过复选框决定在发生确定事件时想要接收的消息,例如

field :when_receive_order, :type => Boolean, :default => "true"
field :when_accept_order, :type => Boolean, :default => "true"
field :when_cancel_order, :type => Boolean, :default => "true"
field :when_reject_order, :type => Boolean, :default => "true"
.
.
.
more...

I want to know if is good use Boolean values or use state_machine gem in these case. 我想知道在这些情况下是否使用Boolean值或使用state_machine gem

In this link http://railstips.org/blog/archives/2012/10/10/booleans-are-baaaaaaaaaad/ is argued that Boolean values are bad. 在这个链接中, http ://railstips.org/blog/archives/2012/10/10/booleans-are-baaaaaaaaaad/认为Boolean值很差。 So I make this question. 所以我提出这个问题。

Note: My database is mongodb and my odm es mongoid 注意:我的数据库是mongodb,而我的odm是mongoid

If there is only one of those boolean values to be true at all times, you can use state_machine. 如果这些布尔值中只有一个始终为true,则可以使用state_machine。 Otherwise, you cannot. 否则,你不能。

In your case, I think it is possible that more than one values could be true. 在您的情况下,我认为可能有多个值可能是真的。 What if a user want to receive mails both when an order is accepted and when an order is canceled? 如果用户想要在接受订单和取消订单时收到邮件,该怎么办? So you cannot use state_machine. 所以你不能使用state_machine。

你是对的,你的实体的这种配置容易导致错误...你应该依赖这个宝石或者看看它的源代码并自己实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM