对于我的应用程序,我有不同的注册入口点,以不同的方式验证事物。
因此,在主注册中,除了电子邮件和密码字段外,什么都不需要。在替代注册字段中,需要更多。所以在用户模型中我有
validate_presence_of :blah, :lah, :foo, :bah, :if => :flag_detected
def flag_detected
!self.flag.nil?
end
我想通过 Controller 设置该标志。但是,该标志不是数据库字段。我只是想知道这是否可以在 Rails 中实现,或者我的想法有问题?如果是这样,实现这一目标的最佳方法是什么?谢谢。
最佳答案
您需要的是attr_accessor
class User < ActiveRecord::Base
attr_accessor :flag
attr_accessible :flag # if you have used attr_accessible or attr_protected else where and you are going to set this field during mass-assignment. If you are going to do user.flag = true in your controller's action, then no need this line
end
基本上
attr_accessor :flag
创建 user.flag
和 user.flag = ...
模型的方法。和
attr_accessible
用于批量赋值保护。
关于ruby-on-rails - Rails 如何设置不是数据库字段的临时变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9697041/