Ruby on Rails
Friday, January 6, 2017
Thank you Tamosus pontik.
I have done the task using this keyword require "#{dynamic_model_name}"
On Thu, Jan 5, 2017 at 7:24 PM, tamouse pontiki <tamouse.lists@gmail.com> wrote:
--On Thu, Jan 5, 2017 at 2:40 AM Daynthan Kabilan <dayanthan86@gmail.com> wrote:Hi Tamosus pontik,Thanks for your reply.The model file is created dynamically based on this code.And after you write that file, you need to require it for Rails to use it. That's why the constant is undefined.Do you know how the require statement works in Ruby?To view this discussion on the web visit https://groups.google.com/d/model_file = File.join("app", "models", fname.singularize+".rb")model_name = fname.singularize.capitalizeFile.open(model_file, "w+") do |f|f << "class #{model_name} < ActiveRecord::Base\nend"endBut i need to store the values to specific tables.Note: I am not running migration for the dynamic tables(model)On Wed, Jan 4, 2017 at 10:07 PM, tamouse pontiki <tamouse.lists@gmail.com> wrote:On Wed, Jan 4, 2017 at 8:57 AM Daynthan Kabilan <dayanthan86@gmail.com> wrote:Hi,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 docreate_table "#{fname}" do |t|#(define your columns just as you would in a migration file)head.each do |h|t.text hend# t.string :my_stringendendmodel_file = File.join("app", "models", fname.singularize+".rb")model_name = fname.singularize.capitalizeFile.open(model_file, "w+") do |f|f << "class #{model_name} < ActiveRecord::Base\nend"end# ss=model_name.newp ss=model_name.constantize.new.............................file="#{fname}_#{cnt}"return fileend
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 ?I do not know if this will work, but you probably need to manually require the model file you create before you try to constantize the model name. I don't think Rails autoloading is working in this situation.--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/CAHUC_ .t9d3gqinFY2hSMDMm8- Z49cQy7cRp4J8Nwgf3mAm4gccg% 40mail.gmail.com --
For more options, visit https://groups.google.com/d/optout .
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 .msgid/rubyonrails-talk/ .CADKeJyQKcRGM% 3Dj3miANP9PqfODZ6fA8x98F8vUfG_ QQ1KnEntw%40mail.gmail.com
For more options, visit https://groups.google.com/d/optout .
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/CAHUC_ .t8QDyZZhv0Z4TANe1Zc7jcR% 2BqYsW4iitPxLQz%3DGqXMpKw% 40mail.gmail.com
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/CADKeJyTPY4LDqJtsHw7RzvFasZeGFhNSudxgm%3DKYYm%2B74uEawQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment