Rails的如何设置一个临时变量,这不是一个数据库字段 [英] Rails how to set a temporary variable that's not a database field

查看:134
本文介绍了Rails的如何设置一个临时变量,这不是一个数据库字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的应用程序,我有一个验证不同的事情不同的注册入口点。

For my app, I have different signup entry points that validate things differently.

因此​​,在主注册,没有什么需要除了电子邮件和密码字段。在一个替代注册字段,有更多的要求。因此,在用户模式,我有

So in the main signup, nothing is required except for the email and password field. In an alternative signup field, many more are required. So in the user model I have

validate_presence_of :blah, :lah, :foo, :bah, :if => :flag_detected

def flag_detected
  !self.flag.nil?
end

欲通过控制器来设置该标志。但是,这种标志是不是数据库字段。我只是想知道,如果这是可以实现的栏杆或有什么问题,我想这个问题的方法?如果是这样,什么是实现这一目标的最佳途径?谢谢你。

I want to set that flag through the controller. However that flag isn't a database field. I'm just wondering if this is achievable in Rails or there is something wrong with the way that I am thinking about this? If so, what's the best way to achieve this? Thanks.

推荐答案

您需要的是 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:标志创建 user.flag user.flag =。 .. 方法模型。

attr_accessible 是面向大众分配的保障。

and attr_accessible is for mass-assignment protection.

这篇关于Rails的如何设置一个临时变量,这不是一个数据库字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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