开发者

Creating a variable on an ActiveRecord model

开发者 https://www.devze.com 2023-03-07 13:53 出处:网络
Somewhat simple quetion I think, but didn\'t really find an answer on google.. What I want is set the current_user for a model..

Somewhat simple quetion I think, but didn't really find an answer on google..

What I want is set the current_user for a model.. So开发者_运维知识库 I can do different validations per user..

Something like this:

var current_user # <= this is what i need

validates, :something, :presence => { :if => :current_user_is_admin? } 

def current_user_is_admin?
    current_user.user_type == 1
end

So in the controller I can set the current_user before saving the record..

Hope it makes sense..

Thanks!


"var" doesn't really make sense, because of Ruby's slapdash style. attr_accessor may be what you want:

attr_accessor :current_user

that defines a reader and a writer method for current_user.

0

精彩评论

暂无评论...
验证码 换一张
取 消