Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access my rails database from a task in lib/tasks?

I am developing an app that needs to send text messages, so I have carrier information stored in a database. I also need that information in an XML file for client side code to read. To make this happen, I am writing a script that reads the carrier information from the DB and creates an XML file in the config directory. I felt this script would fit best in lib/tasks.

I need to access the database from this script, but I want to use some object to access it. If I use

db = Mysql.new("domain", "username", "password", "database")

I will have to keep multiple versions for different environments because I do not use MySQL all the time. That would be very sloppy. I am sure there is a way to do this. I tried to just access the object...this is what I have so far:

RAILS_HOME = File.expand_path(File.join(File.dirname(__FILE__),"../.."))
RAILS_CONFIG = "#{RAILS_HOME}/config"

f = File.new("#{RAILS_CONFIG}/mls_widget_config.xml", "w")
carriers = Carrier.find_all
f.write carriers
f.close

But Carrier is not defined, which makes sense. How can I give this script access to the the Carrier object in the DB?

Also as a side, if anyone knows how to easily convert what I read from the DB into proper XML that would be great. I was going to write something custom real quick.

Thank you!

like image 719
Tony Avatar asked Apr 04 '09 15:04

Tony


People also ask

How do I see rake tasks?

You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing rake --tasks . Each task has a description, and should help you find the thing you need.

What is Rakefile in rails?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.

How do I run a rake task in rails?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.

What does rake routes do?

Typically the core libraries provide Rail ties which expose tasks. rake routes first runs the Rakefile , which calls AppName::Application::load_tasks , which creates a task called :routes , which is then executed since it was passed as argument to rake .


1 Answers

You can enable a Rake task to access your models by defining your task like this:

task :my_task => :environment do
  # Task code
end

Note the => :environment, which grants this access. You can then instruct your Rake task to use different environments this way:

rake RAILS_ENV=development my_task
rake RAILS_ENV=production my_task

As for XML serialization, you can use the built-in to_xml method, such as:

Carrier.all.to_xml

Note that the method .all is a recent addition to Rails, and is an alias for .find(:all).

like image 91
Ron DeVera Avatar answered Sep 20 '22 01:09

Ron DeVera