在 Rails 中命名布尔列 [英] Naming Boolean columns in Rails

查看:51
本文介绍了在 Rails 中命名布尔列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 Dog,我想在 Rails 中存储它是否经过 trained.通常,返回布尔值的 Ruby 方法的名称以 ? 结尾.我应该调用数据库列trained?,还是应该调用数据库列trained并有方法

Let's say I have a Dog and I want to store if it is trained in Rails. Conventionally, Ruby methods that return booleans have names that end with ?. Should I call the database column trained?, or should I call the database column trained and have a method

class Dog
  def trained?
    trained
  end
end

后一种选择似乎效率低下,尤其是当我有很多布尔字段时.

The latter option seems inefficient, particularly when I have lots of boolean fields.

或者还有其他我遗漏的选择吗?

Or is there some other alternative I'm missing?

推荐答案

你应该称之为 trained.使用 :boolean 类型在您的架构中定义它.您可以将其称为 trained?,一切都会神奇地工作.

You should call it trained. Define it in your schema with a type of :boolean. You can refer to it as trained? and everything will magically work.

如是说http://www.ruby-forum.com/topic/60847

这篇关于在 Rails 中命名布尔列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆