简体   繁体   English

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

[英]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. 我还需要XML文件中的信息来读取客户端代码。 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. 为了实现这一点,我正在编写一个脚本,从DB读取运营商信息并在config目录中创建XML文件。 I felt this script would fit best in lib/tasks. 我觉得这个脚本最适合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. 我将不得不为不同的环境保留多个版本,因为我不会一直使用MySQL。 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. 但Carrier没有定义,这是有道理的。 How can I give this script access to the the Carrier object in the DB? 如何让这个脚本访问DB中的Carrier对象?

Also as a side, if anyone knows how to easily convert what I read from the DB into proper XML that would be great. 另外作为一方,如果有人知道如何轻松地将我从数据库中读取的内容转换为适当的XML,那将是非常好的。 I was going to write something custom real quick. 我打算快速写一些自定义的东西。

Thank you! 谢谢!

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. 请注意=> :environment ,它授予此访问权限。 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)的别名。

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; Rails还包括通过使用to_xml方法调用的默认XML序列化; try Carrier.find(:all).to_xml . 尝试Carrier.find(:all).to_xml

By convention, lib/tasks is usually reserved for rake tasks - you might want to put your library code in its own directory. 按照惯例,lib / tasks通常保留用于rake任务 - 您可能希望将库代码放在自己的目录中。 lib/messaging, maybe? lib / messaging,也许吧?

Are you running an old version of Rails? 你在运行旧版Rails吗? find_all doesn't work in recent versions: 'find(:all)' or just 'all' are the methods nowadays. 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM