1

Controller ClientDocument.

def upload_document
  ClientDocument.upload_client_document(params)
end

Model ClientDocument.

Class method..

def self.upload_client_document(params)
  self.new :uploaded_data => params[:Filedata],:client_id => params[:client_id]
  rename_document_name(params) # Call instance method
end

Instance method..

def rename_document_name(params)
  self.filename = "#{self.client.client_no}-#{self.filename}"
end

Is it possible to call instance method from class method ?

Before storing into database i want to rename filename.

Which is the proper way to solve out this problem?

1 Answer 1

4
def self.upload_client_document(params)
  instance = self.new :uploaded_data => params[:Filedata],:client_id => params[:client_id]
  instance.rename_document_name(params) # Call instance method
  instance
end
Sign up to request clarification or add additional context in comments.

2 Comments

Will it call before_save and all callbacks with this code ?? I need to store object like instance.save! or automatically it will store ??
@krunal: Nothing will get stored in the above code. If you want to save the object, call instance.save at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.