在Rails / ActiveRecord列名中使用问号字符

时间:2021-04-09 22:24:44

In keeping with Ruby's idiom of using a question mark in boolean methods (e.g. person.is_smart?), I'd like to do the same for an ActiveRecord field in Rails:

为了与Ruby在布尔方法中使用问号的习惯(例如person.is_smart?)保持一致,我想对Rails中的ActiveRecord字段做同样的事情:

rails generate model Person is_smart?:boolean

I haven't actually run the above statement. I assume that database fields can't have a question mark in them. Will rails deal with this appropriately? Is the best practice to simply leave question marks off of models?

我实际上没有运行上述声明。我假设数据库字段中不能有问号。 rails会适当地处理这个吗?简单地从模型中留下问号是最佳做法吗?

Using Rails 3.2.8

使用Rails 3.2.8

3 个解决方案

#1


76  

Rails will automatically generate the method smart? if there is a field named 'smart'.

Rails会自动生成智能方法吗?如果有一个名为'smart'的字段。

#2


0  

Actually, Im using Rails 4 and I can't call my boolean column without the question mark

实际上,我使用Rails 4,我不能在没有问号的情况下调用我的布尔列

pry(main)> User.where(is_validated: false).first.is_validated
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."is_validated" = 'f' ORDER BY "users"."id" ASC LIMIT 1
=> nil
[13] pry(main)> User.where(is_validated: false).first.is_validated?
  User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."is_validated" = 'f' ORDER BY "users"."id" ASC LIMIT 1
=> false

#3


0  

One "gotcha" to be aware of if you happen to use :enum in your model, since this stores the value as an integer. The question mark attr method provided by active record expects to evaluate 0 or 1 as false / true respectively in the database. For example:

如果您碰巧使用了一个“问题”:模型中的枚举,因为它将值存储为整数。由活动记录提供的问号attr方法期望在数据库中分别将0或1评估为false / true。例如:

class Person
  enum mood: ['happy', 'sad', 'bored']
end

p = Person.new(mood: 'happy') # this would store mood as 0 in db
p.mood? #=> false

p.mood = 'sad' # saves as 1 in db
p.mood? #=> true

p.mood = 'bored' # saves as 2 in db
p.mood? #=> true

to see how this method works, see rails source

要了解此方法的工作原理,请参阅rails source

#1


76  

Rails will automatically generate the method smart? if there is a field named 'smart'.

Rails会自动生成智能方法吗?如果有一个名为'smart'的字段。

#2


0  

Actually, Im using Rails 4 and I can't call my boolean column without the question mark

实际上,我使用Rails 4,我不能在没有问号的情况下调用我的布尔列

pry(main)> User.where(is_validated: false).first.is_validated
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."is_validated" = 'f' ORDER BY "users"."id" ASC LIMIT 1
=> nil
[13] pry(main)> User.where(is_validated: false).first.is_validated?
  User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."is_validated" = 'f' ORDER BY "users"."id" ASC LIMIT 1
=> false

#3


0  

One "gotcha" to be aware of if you happen to use :enum in your model, since this stores the value as an integer. The question mark attr method provided by active record expects to evaluate 0 or 1 as false / true respectively in the database. For example:

如果您碰巧使用了一个“问题”:模型中的枚举,因为它将值存储为整数。由活动记录提供的问号attr方法期望在数据库中分别将0或1评估为false / true。例如:

class Person
  enum mood: ['happy', 'sad', 'bored']
end

p = Person.new(mood: 'happy') # this would store mood as 0 in db
p.mood? #=> false

p.mood = 'sad' # saves as 1 in db
p.mood? #=> true

p.mood = 'bored' # saves as 2 in db
p.mood? #=> true

to see how this method works, see rails source

要了解此方法的工作原理,请参阅rails source