jmasterx
jmasterx

Reputation: 54193

Run a script that uses my models

I have the following need.

I have a Rails web application. I need to write a script that will read the excel file, and call methods of my model to fill the database.

This is not a problem.

I am just wondering:

Is there something a bit like seed.rb where I can just run the .rb file and it will just make the changes?

like: rails runmyfile myfile.rb

That file will read the spreadsheet and input the data. I would use seed.rb, but that will not let me add things while the server is running.

Upvotes: 1

Views: 251

Answers (5)

NM Pennypacker
NM Pennypacker

Reputation: 6952

You can do something like this with the CSV library and incorporate it into a rake task:

desc "add models from csv file"
task :add_csv_models => :environment do
  require 'csv'
  CSV.foreach('path/to/your_file.csv') do |row|
    mod = YourModel.new(attribute_1: row[0], attribute_2: row[1])
      if mod.save
        printf "\nModel Saved"
      else
        # handle exception here
      end
  end
end

Then run rake:add_csv_models You'll have to tweak the attributes and row indexes, but this should give you the general idea.

Upvotes: 0

Arup Rakshit
Arup Rakshit

Reputation: 118299

Yes rails runner is what you are looking for.

$ bin/rails runner  -e staging myfile.rb

Read the usuage from your console :

$ bin/rails runner
Usage: rails runner [options] [<'Some.ruby(code)'> | <filename.rb>]

    -e, --environment=name           Specifies the environment for the runner to operate under (test/development/production).
                                     Default: development

    -h, --help                       Show this help message.

Examples:
    rails runner 'puts Rails.env'
        This runs the code `puts Rails.env` after loading the app

    rails runner path/to/filename.rb
        This runs the Ruby file located at `path/to/filename.rb` after loading the app

You can also use runner as a shebang line for your executables:
    -------------------------------------------------------------
    #!/usr/bin/env /Users/xxx/rails/app/bin/rails runner

    Product.all.each { |p| p.price *= 2 ; p.save! }
    -------------------------------------------------------------

Upvotes: 1

Mathias Vonende
Mathias Vonende

Reputation: 1380

I use rails runner for this purpose. The runner runs a snippet of code directly, but with a full fledged rails environment. If you want to run a script (assuming your script is under "lib/myscript.rb"), in a rails-environment, then run:

rails runner lib/myscript.rb

If you want to run the script in a specific environment, then use the -e parameter:

rails runner -e development lib/myscript.rb

Upvotes: 0

Piotr Kruczek
Piotr Kruczek

Reputation: 2390

Try creating a rake task for it. Then just

rake my_tasks:task_name

More info on custom rake tasks can be found here

Upvotes: 5

Kristj&#225;n
Kristj&#225;n

Reputation: 18843

You're looking for rails runner, which runs any Ruby script in your rails environment, giving you access to your models, etc.

$ echo 'puts "hi"' > test.rb
$ rails runner test.rb
hi

Or run a small snippet directly from the command line.

$ rails runner 'puts "hi"'
hi

Upvotes: 0

Related Questions