耙子流产了!对于确实存在的未初始化常数

时间:2013-12-26 17:19:22

标签: ruby-on-rails ruby rake rake-task

我已经建立了一个rake任务 -

# SET RAKE TASK NAMESPACE
namespace :db do
# RAKE TASK DESCRIPTION
desc "Fetch property information and insert it into the database"

# RAKE TASK NAME    
task :insert_properties do

    # REQUIRE LIBRARIES
    require 'nokogiri'
    require 'open-uri'

    # OPEN THE XML FILE
    mits_feed = File.open("app/assets/xml/mits.xml")

    # OUTPUT THE XML DOCUMENT
    doc = Nokogiri::XML(mits_feed)

    # FIND PROPERTIES OWNED BY NORTHSTEPPE AND CYCLE THORUGH THEM
    doc.xpath("//Property/PropertyID/Identification[@OrganizationName='northsteppe']").each do |property|

        # GATHER EACH PROPERTY'S INFORMATION
        information = {
            "street_address" => property.xpath("/Address/AddressLine1/text()"),
            "city" => property.xpath("/Address/City/text()"),
            "zipcode" => property.xpath("/Address/PostalCode/text()"),
            "short_description" => property.xpath("/Information/ShortDescription/text()"),
            "long_description" => property.xpath("Information/LongDescription/text()"),
            "rent" => property.xpath("/Information/Rents/StandardRent/text()"),
            "application_fee" => property.xpath("/Fee/ApplicationFee/text()"),
            "bedrooms" => property.xpath("/Floorplan/Room[@RoomType='Bedroom']/Count/text()"),
            "bathrooms" => property.xpath("/Floorplan/Room[@RoomType='Bathroom']/Count/text()"),
            "bathrooms" => property.xpath("/ILS_Unit/Availability/VacancyClass/text()")
        }


        # CREATE NEW PROPERTY WITH INFORMATION HASH CREATED ABOVE
        Property.create!(information)

    end # ENDS XPATH EACH LOOP

end # ENDS INSERT_PROPERTIES RAKE TASK

end # ENDS NAMESAPCE DECLARATION

当我运行“rake db:insert_properties”时,这是我生成的错误 -

rake aborted!
uninitialized constant Property
/Users/stevejobs/Sites/nsrosu/lib/tasks/property_information.rake:39:in `block (3 levels) in <top (required)>'
/Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.1/lib/nokogiri/xml/node_set.rb:237:in `block in each'
/Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.1/lib/nokogiri/xml/node_set.rb:236:in `upto'
/Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.1/lib/nokogiri/xml/node_set.rb:236:in `each'
/Users/stevejobs/Sites/nsrosu/lib/tasks/property_information.rake:21:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:insert_properties
(See full trace by running task with --trace)

我有一个Property模型,其中一个表设置为接受所有这些属性,为什么我会收到此错误?

1 个答案:

答案 0 :(得分:6)

您的rake任务需要在运行之前启动Rails环境。这样你就可以访问这些类了。

为此,您需要运行environment任务。要实现此目的,请将任务定义更改为

task :insert_properties => :environment do