开发者

Advice on methods for custom validation

开发者 https://www.devze.com 2023-02-07 05:03 出处:网络
I am using Ruby on Rails 3 and I would like to know if my approach to validate new record is good or not.

I am using Ruby on Rails 3 and I would like to know if my approach to validate new record is good or not.

I don't use the common RoR validation system开发者_运维技巧, so in my model I have all custom validation mathods like these:

  def validates_user_name(user)
    ...
  end

  def validates_user_surname(user)
    ...
  end

  ...

that I call from controller in this way

def create
  ...
  @user.validates_user_name(params[:user])
  @user.validates_user_name(params[:user])
  ...
end

Is it a good way to validate the creation of new user? There will be problems with hackers using this approach?


I think you're going to have a hard time convincing anyone that your custom validations are better than what's built into Rails, especially if the validation logic is similar.

If you still want control over when things happen, you should take advantage of the built-in callback hooks like before_create. There are lots of advantages of doing it this way, including automatic transaction rollback and decoupling. However, if what you're doing is already accomplished by Rails, it's not advisable to reinvent the wheel.

0

精彩评论

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