4

I am using this file-uploader for ruby on rails. I upload some files with numbers for statistical analysis. Model for the files is called filedb.In filedb.rb the file is opened and numbers are analysed(some correlation stuff and etc.). After that I need so save the results into a table called results.

Will it be clever just to write in filedb.rb:

@cell=Results.new(:cell_name =>filenames, :icorrelation=>intensities)
@cell.save

Or it is better to use results_controller to create a record in a table? and to to something like this: results_controller.rb:

  def create
    @result = Result.new(params[:result])    
    if @result.save
     lalala     
    else
      render :new
    end
  end

Though I do not know how to pass arguments :result to a controller

Thanks in advance

edit:

filedbs_controller.rb:

  def analyse

   (filedb.where(analyse:no)).perform_analysis

     respond_to do |format|
       format.html { redirect_to :back }
     end

  end

filedb.rb

def self.perform_analysis
   list=Analysis.do_number_analyse
   if list!=nil   
   results(list)
 end

  end


      def self.results(list)

       do somthing with list
        cell=Results.new(:cell_name =>filenames, :icorrelation=>intensities)
        cell.save
    end

Analysis.do_number_analyse - is a method in another model, where all calculations are done

2 Answers 2

3

Your first approach should be ok, you can save the Result from the File model.

You do not need to use the instance variable @, as it is thought to send the variable to the view, and I can not imagine why you need so here.

To sum up, in filedb.rb:

# here you calculations and then
cell = Result.new(cell_name: filenames, icorrelation: intensities)
cell.save!

Be careful with the model, should be in singular (Result, and not Results). And if you use the save! method, with "!" you will see any errors thrown by the controller when saving the new cell.

Hope that helps

EDIT AFTER KATJA EDIT

I am a bit lost about how the analyse action in the FieldsController is reached, I guess that you are sending the browser there after a successful file upload. Assuming that the code that you have exposed should work properly.

Nevertheless, it is true that it seems a bit complicated and perhaps you could reach the same result in a simpler way. As I do not know your whole code perhaps I am missing something but what I do is get rid of the analyse action and deal with the cell creation through an after_create callback inside the Filedb model.

class Filedb < ActiveRecord::Base
  # associations, validation and accessible stuff goes here, and then:

  after_create :perform_analysis

  protected
  def perform_analysis
    list = Analysis.do_number_analyse
    results(list) unless list.nil?
  end

  def results(list)
  # I assume that here you are using 'list' to get 'filenames' and 'intensities' values, and then:
    cell = Result.new(cell_name: filenames, icorrelation: intensities)
    cell.save!
  end
end

The after_create callback is triggered just on instance creation, so here is better than after_save.

That way, you do not need any 'analyse' action in the controller, as the 'perform_analysis' method will be called automatically after each file creation; your code is tight together in the model and you can see the flow easily in the future if you need to come back and change something.

Makes sense?

Sign up to request clarification or add additional context in comments.

5 Comments

but it will be a clever way to do what I want or the controller-story is better for that?
I think that your approach if perfectly fine.
@Katja could you please share your fieldb.rb model file? I assumed in my answer that you were doing your calculations inside a callback, otherwise that could should go in the fieldb controller.
@Katja I have edited the answer based on your feedback, hope now it's clearer
@Galen it makes sense unless I do not need after_create callback as the user should define when the analysis should be performed (it is a long story why) but since the creation of the new record in results table is not effected, I like your answer=)
0

Did @user exist? if not @user is not defined. That will give you error.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.