rishabh agarwal
rishabh agarwal

Reputation: 99

How to create a script in rails and run it using console

I am trying to modify my database in rails for that I want to write a script that would modify my model accordingly when executed. I have no idea how to create a script and run it using rails console. Please somebody guide me.

Eg -: Suppose i want to write a script that has Model.all written in it and when I execute it using console Model.all should run

Upvotes: 0

Views: 1945

Answers (2)

Nick M
Nick M

Reputation: 2522

Use Rails tasks instead:

lib/tasks/mytasks.rake

namespace :mytasks do

    desc "This is a Hello world task. All it does it say hello"
    task :hello => :environment do
      puts "Hello!"
    end
end

Then in the console you do:

rake mytasks:hello

Upvotes: 2

Alex Kojin
Alex Kojin

Reputation: 5204

One the quick temporary solution adds a new class method to your Model:

class Model
  def self.modify_db
    Model.find_each do |item| 
    end
  end
end

In console just type: Model.modify_db

Upvotes: 0

Related Questions