Using question mark character in Rails/ActiveRecord column name

前端 未结 3 353
长情又很酷
长情又很酷 2021-02-01 01:45

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:

3条回答
  •  猫巷女王i
    2021-02-01 02:39

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

    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
    

提交回复
热议问题