Ruby on Rails
Wednesday, January 4, 2017
Hi,
How can declare the object for dynamic model. Because I couldn't store the values to new table.
-- I have a CSV upload option and need to create a table and model based on the CSV colums.
We allow to upload different kind of CSV file formats and each file upload will create new table and info will be stored accordingly. I have created a table with columns based on CSV file header and also created a model base on CSV file name. But when i store the file details to specific table i faced error.
def self.import(file, head, csvalue,fname)
...............
..............
ActiveRecord::Schema.define do
create_table "#{fname}" do |t|
#(define your columns just as you would in a migration file)
head.each do |h|
t.text h
end
# t.string :my_string
end
end
model_file = File.join("app", "models", fname.singularize+".rb")
model_name = fname.singularize.capitalize
File.open(model_file, "w+") do |f|
f << "class #{model_name} < ActiveRecord::Base\nend"
end
# ss=model_name.new
p ss=model_name.constantize.new
..............
...............
file="#{fname}_#{cnt}"
return file
end
How can declare the object for dynamic model. Because I couldn't store the values to new table.
I faced the Error like NameError (uninitialized constant Mytest):
Any idea how to solve this ?
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CADKeJyQ6fmEyY82x2-U9L3WuA7NR%2B93JaZjTTZyY4teU%3DXTLTg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment