Rails(validate:false),除了一个属性

时间:2016-11-11 16:04:02

标签: ruby-on-rails

有没有办法object.save(validate: false)指定我们实际想要验证的属性?

因此,例如,对于User模型,除了我想要验证的名称之外,我想要validate: false

2 个答案:

答案 0 :(得分:0)

一种复杂的方法是在没有验证的情况下执行object.save,然后使用update更新应该验证的属性。将跳过所有其他验证,并且仅验证您所需的属性:

# save all other attributes other than the one that needs validation
object.assign_attributes params.except(:required_attribute)
object.save validate: false

if object.persisted?
  # update the attribute which requires validation
  object.update required_attribute: params[:required_attribute]

  if ! object.valid?
    # handle validation error
  end
end

答案 1 :(得分:0)

我如何做:

我只需要验证from __future__ import print_function import pickle import os.path from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request # If modifying these scopes, delete the file token.pickle. SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/drive.readonly', 'https://www.googleapis.com/auth/drive.metadata.readonly', 'https://www.googleapis.com/auth/drive.appdata', 'https://www.googleapis.com/auth/drive.metadata', 'https://www.googleapis.com/auth/drive.photos.readonly'] def main(): """Shows basic usage of the Drive v3 API. Prints the names and ids of the first 10 files the user has access to. """ creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open('token.pickle', 'wb') as token: pickle.dump(creds, token) service = build('drive', 'v3', credentials=creds) # Call the Drive v3 API queries = [ "mimeType = 'application/vnd.google-apps.folder'", "'my_email@gmail.com' in owners" ] drive_str_query = queries[0] if len(queries) == 1 else " and ".join(queries) results = service.files().list(q=drive_str_query, pageSize=1000, spaces='drive', fields="nextPageToken, files(*)").execute() items = results.get('files', []) if not items: print('No files found.') else: print('Files:') for item in items: print(item) if __name__ == '__main__': main() 模型的1个属性。但是我没有使用User模型进行此验证。

我创建的User模型仅用于验证。

AutoRegisterUser

这里我正在使用class AutoRegisterUser < ApplicationRecord self.table_name = 'users' attr_accessor :business_booking validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP, message: 'has invalid format' } validates :email, uniqueness: { message: 'has already been taken' } end 数据库表。因此,我可以从self.table_name = 'users'数据库表中检查uniqueness的验证。

现在,我可以这样验证:

users

我希望此解决方案可以帮助某人。