开发者

Common function for self.errors.add_to_base in rails

In my every model i have 10 or more lines for self.errors.add_to_base. is there any other way i can manage those lines in more easy way? will it possible to manage 开发者_Python百科those errors with some common function which can handle self.errors.add_to_base for any of my model?

 Function like
 def error_add_to_base(message,conditions)
   self.errors.add_to_base(message) if eval("#{conditions}")
 end


I think that OP asks, if a rails helper method exists, which can be called instead of defining callbacks for each model, where a need for a (custom) validation occurs. So instead of using 'validate' helper, he would call such helper to report errors that don’t tie to any specific attribute, but to a model instance, "as a whole".

And If I was to answer his question: No, no such "common function" exists. What's wrong with:

validate :must_meet_my_business_logic

def :must_meet_my_business_logic
errors.add(:base, "blah!") if...
end

anyway?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜