Access a model from a Ruby file in Rails? -


i wrote own code supposed create new performance (model) entry in database. lack basic understanding of how supposed work dont know search either.

i placed file in projects root directory. when run file command-line following error:

    c:/rubyprojects/dads/fileparser.rb:17:in `<main>': uninitialized constant performance (nameerror) 

this code:

 require 'date' #require 'performance' #require 'performer'  file_name = file.basename("app\assets\documents\location-2003.11.11-artistsname-artistname2.txt", ".txt")  performance_array = file_name.split("-")  location = performance_array[0] date = performance_array[1]   begin    date = date.parse(date) rescue argumenterror    date = date.parse("31-02-2010") end  #need access performance here fails performance = performance.create(file_name: file_name,                                        date: date,                                   location: location)   performance_array.drop(2).each |performer_name|    puts artist_name    #save performer in db if doesnt exist   performer = performer.find_by_name(performer_name)   if performer == nil     performer = performer.create(name: performer_name)    end    #create performer performance relationship   performance.performers << performer end 

this model file \app\models\performance.rb:

class performance < activerecord::base    validates :file_name, presence: true, length: { maximum: 50 },  uniqueness: { case_sensitive: false }    validates :date, length: { maximum: 30 }      validates :location, length: { maximum: 50 }    has_many :performer_performances, dependent: :destroy      has_many :performers, through: :performer_performances     def has_performer?(performer)       performer_performances.find_by(performer_id: performer.id)    end     def include! (performer)      performer_performances.create!(performer_id: performer.id)    end  end 

run file using

$ rails runner fileparser.rb 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -