Alina
Alina

Reputation: 2261

Ruby on Rails, create a record from another model

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

Upvotes: 4

Views: 8157

Answers (2)

Galen
Galen

Reputation: 947

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?

Upvotes: 3

Lian
Lian

Reputation: 1629

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

Upvotes: 0

Related Questions