I have a model:
class HelloRails < ActiveRecord::Base
attr_accessible :filename, :filevalidate
include In4systemsModelCommon
validates :filename, presence: true
def update
parameters = [self.filename, @current_user]
parameters.map!{|p| ActiveRecord::Base.connection.quote p}
sql = "call stored_procedure(#{parameters.join(',')})"
ActiveRecord::Base.connection.execute(sql)
end
end
In the view I have a text_field called as :filename. When I click the submit button it calls this update method in model to call the stored proc to execute. Now validations are not working.
I dont want to accept nil for filename. How can I do this?
It doesn’t validate because you are executing sql directly:
Validations are only run when you use the “normal” ActiveRecord methods like
saveandupdate_attributes. To run validations manually you can callvalid?on your object.Model:
Then in your controller you have to check wether
@object.updatereturnstrueorfalseand display errors in your view if necessary.Controller: