I have a question. I’m parsing content using Nokogiri and my objective is to process this content and insert in the database.
Table_rows contains parsed tr/tr elements and then I do Nokogiri’s node.content.to_a to get the td values I want.
(note: I need this column rec_id because I don’t know yet how to force id value when I insert hehehehe)
The code below generates an exception when it tries to save
table_rows.each do |row|
rec_id = row.content.to_a[0].strip.to_i
if MyModelName.where(:rec_id => rec_id) == []
MyModelName.init_from_array(row.content.to_a).save
end
end
init_from_array contains simple code like this:
def self.init_from_array(array)
@mymodelname = MyModelName.new
@mymodelname.field1 = array[0].strip #strip to avoid any empty
@mymodelname.field2 = array[1].strip
#and so on...
@mymodelname
end
Inside controller I’m just calling this method between a begin and rescue. MyModelName don’t have any validation for now.
What I’m doing wrong to get this behavior – it generates a “cannot rollback, no transaction is active” but it saves the record to the database.
edit:
here’s the exception
SQLite3::SQLException: cannot rollback – no transaction is active from /opt/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.3.2/lib/sqlite3/database.rb:97:in close’ from /opt/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.3.2/lib/sqlite3/database.rb:97:inprepare’ from /opt/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.3.2/lib/sqlite3/database.rb:134:in execute’ from
Solved the problem.
The problem was with my migration file. Everything ran without problems but I had 1 migration for table creation and then more 3 migrations that added fields to this model. I don’t know yet how or why but I deleted all the migrations and started from scratch and now it saves without that error.
Thanks