如何从lib/任务的任务中访问rails数据库?

时间:2022-05-02 07:11:11

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.

我正在开发一个需要发送短信的应用程序,所以我有存储在数据库中的传送信息。我还需要在XML文件中读取客户端代码。为了实现这一点,我正在编写一个脚本,从DB中读取载体信息,并在config目录中创建一个XML文件。我觉得这个脚本最适合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:

我将不得不为不同的环境保留多个版本,因为我不会一直使用MySQL。那太草率了。我肯定有办法做到这一点。我试着访问这个对象…这是我目前所拥有的:

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?

但是载波没有定义,这是有意义的。如何让这个脚本访问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.

另外,如果有人知道如何轻松地将我从DB中读到的内容转换成合适的XML,那就太棒了。我很快就会写一些自定义的东西。

Thank you!

谢谢你!

3 个解决方案

#1


8  

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

通过这样定义任务,您可以启用Rake任务来访问模型:

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任务以这种方式使用不同的环境:

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:

至于XML序列化,可以使用内置的to_xml方法,例如:

Carrier.all.to_xml

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

注意,这个方法.all是Rails的最新添加,是.find(:all)的别名。

#2


2  

You're actually almost there; I'd just recommend requiring your Rails environment as part of the script, like so:

你实际上几乎;我建议您将Rails环境作为脚本的一部分,如下所示:

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

Now you should have access to all of your domain structure. Rails also includes default XML serialization through the use of the to_xml method call; try Carrier.find(:all).to_xml.

现在您应该可以访问所有的域结构。Rails还通过使用to_xml方法调用来包含默认的XML序列化;尝试Carrier.find .to_xml(所有)。

#3


2  

By convention, lib/tasks is usually reserved for rake tasks - you might want to put your library code in its own directory. lib/messaging, maybe?

按照惯例,lib/tasks通常是为rake任务预留的——您可能希望将库代码放在它自己的目录中。lib /消息,可能吗?

Are you running an old version of Rails? find_all doesn't work in recent versions: 'find(:all)' or just 'all' are the methods nowadays.

你运行的是旧版本的Rails吗?find_all在最近的版本中不起作用:'find(:all)'或只是'all'是现在的方法。

File.new("#{RAILS_ROOT}/mls_widget_config.xml", "w") do |f|
  Carrier.all.each { |carrier| f.puts carrier.to_xml }
end

#1


8  

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

通过这样定义任务,您可以启用Rake任务来访问模型:

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任务以这种方式使用不同的环境:

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:

至于XML序列化,可以使用内置的to_xml方法,例如:

Carrier.all.to_xml

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

注意,这个方法.all是Rails的最新添加,是.find(:all)的别名。

#2


2  

You're actually almost there; I'd just recommend requiring your Rails environment as part of the script, like so:

你实际上几乎;我建议您将Rails环境作为脚本的一部分,如下所示:

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

Now you should have access to all of your domain structure. Rails also includes default XML serialization through the use of the to_xml method call; try Carrier.find(:all).to_xml.

现在您应该可以访问所有的域结构。Rails还通过使用to_xml方法调用来包含默认的XML序列化;尝试Carrier.find .to_xml(所有)。

#3


2  

By convention, lib/tasks is usually reserved for rake tasks - you might want to put your library code in its own directory. lib/messaging, maybe?

按照惯例,lib/tasks通常是为rake任务预留的——您可能希望将库代码放在它自己的目录中。lib /消息,可能吗?

Are you running an old version of Rails? find_all doesn't work in recent versions: 'find(:all)' or just 'all' are the methods nowadays.

你运行的是旧版本的Rails吗?find_all在最近的版本中不起作用:'find(:all)'或只是'all'是现在的方法。

File.new("#{RAILS_ROOT}/mls_widget_config.xml", "w") do |f|
  Carrier.all.each { |carrier| f.puts carrier.to_xml }
end