Вопрос

I have built a rake task --

# 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

when I run "rake db:insert_properties" this is the error I produce --

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)

I have a Property model with a table set up to accept all of these attributes, so why am I getting this error?

Это было полезно?

Решение

Your rake task needs to boot the Rails environment prior to running. That way you can access the classes.

To do this, you need to run the environment task. To make this happen, change your task definition to

task :insert_properties => :environment do
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top