Jump to content

The ultimate community for Ruby on Rails developers.


Creating objects from file posted in a form

  • Please log in to reply
2 replies to this topic

#1 santi_2010



  • Members
  • 7 posts

Posted 31 October 2013 - 03:38 PM

Hi everyone!

I have started learing ruby by myself couple of weeks ago and now I'm trying to do some small app.

I have been searching for the solution of this problem buy I couldn't find it. 


In my models I have a class called TimeRecord. 

In my views I have time_records/upload.erb.html. Here is where I have a form to upload an Excel file. 


What I need is to read that Excel file and insert all the objects in the database.


Then I have this in the controller:

class TimeRecordsController < ApplicationController
  def upload
    if (params[:uploaded_file].present?)
      book = Spreadsheet.open params[:uploaded_file].tempfile
      sheet1 = book.worksheet 0
      sheet1.each 1 do |row|
        @time_record = TimeRecord.create(:employee_id => 1 , :datetime => DateTime.parse(row[3]))

And when I want to try it I get this error (and the @time_record assignment is highlighted): 


ActiveRecord::DangerousAttributeError in TimeRecordsController#upload hash is defined by Active Record

Any ideas what I am doing wrong? Thanks so much for your help!


PS: Im using Ruby 2 and Rails 4 :)

#2 stevieing



  • Members
  • 40 posts

Posted 31 October 2013 - 03:56 PM   Best Answer

The clue is in the error. Hash is a reserved name so you can't use it as an attribute name. It is defined in ActiveRecord or more appropriately it is a ruby Class.


There are hacks but you are better off renaming your attribute. Even if you do continue to use it could cause you all sorts of problems.



  • james likes this

#3 santi_2010



  • Members
  • 7 posts

Posted 31 October 2013 - 04:38 PM

I need to pay more attention to that!

Thanks so much!



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users