views:

48

answers:

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?

+4  A: 
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
sepp2k
@sepp2k 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 shah
@krunal: Nothing will get stored in the above code. If you want to save the object, call `instance.save` at the end.
sepp2k
@sepp2k Thank you ..
krunal shah