Raymond Ruiz-Veve
Raymond Ruiz-Veve

Reputation: 167

Ruby on Rails. Model Associations

I have one Model , file.rb, a controller,filecontroller.rb, and a set of views for a table called files. This was done in order to be able to upload Excel files so that I can view a list in the index view of when someone uploaded an Excel file, the time of its creation and the name of the individual who uploaded the Excel file.

Now, I have another Model, bottle.rb, a controller, bottlecontroller.rb, and a set of views for a table in the database called bottles. The columns in the bottles table in the database are the same as the Excel files.

I already have the ability to upload excel files after some validations so that they can go into the files table in the database. And I also followed the rails cast tutorial to be able to import an excel file from the bottle index view and update my bottles table.

However, I would like to be able to upload an Excel file and import the information in the file index view. But the import needs to also update the information in the bottles table. So in other words, the Excel import from the file index view needs to be able to update the bottle table database.

I know I need an association between my files.rb model and bottles.rb model.. but any help would be awesome!

************Updated Question*****************************************

In my filecontroller.rb, I have added @file.bottles = Bottle.import(params[:file_url]) in the create method after save. I am storing the excel files in:file_url. I also added an association in my file.rb model of has_many :bottles

 def create
    .........
    ..........
    if file.save
    @file.bottles =  Bottle.import(params[:file_url])
    end
end

And in my bottle model bottle.rb I have

def self.import(file_url)
    spreadsheet =  Roo::Excel.new(file_url.path, packed:nil,file_warning: :ignore)
    .........
    bottle.save!
end

But I am getting a undefined method path' for nil:NilClas error in this linespreadsheet = Roo::Excel.new(file_url.path, packed: nil,file_warning: :ignore)` It's not recognizing file_url as the path for the Excel file.

Upvotes: 2

Views: 236

Answers (2)

Raymond Ruiz-Veve
Raymond Ruiz-Veve

Reputation: 167

Okay After if@file.save

I added Bottle.import(@file.file_url.path)

And in the bottle.rb model, I changed

spreadsheet =  Roo::Excel.new(file_url.path, packed: nil,file_warning: :ignore)

to

spreadsheet =  Roo::Excel.new(file_url, packed: nil,file_warning: :ignore)

AND BOOM!

Upvotes: 0

Eugene G
Eugene G

Reputation: 466

two ways: either do it in the controller or use a callback in your model.

If you only need to do this action when the user imports from a specific view, you can handle it within the controller. For example, if uploading the excel file in your index file view calls the 'create' action in files_controller.rb, you can do something like:

if @file.save # update bottle database using data from @file end

however, if you want this to be done every time a new file is created, through the view or not, you can add an after_create callback to your file.rb model. If you want it to be done every time after it's saved, you can do an after_save callback.

Upvotes: 1

Related Questions