0

I'm trying to development a little project in Rails with rake-tasks.

First of all, I get some data from the rake-call and I want to store it in the database. The problem: when I try to store the generated data, I always receive an error (unknown attribute: actuals_cost). But when I try to give it manual in, it works...

Here is some of my code:

desc "Import Projects from CRM"
task :import_projects_crm => [:environment] do
    crm_projects = Crm::Importer.new().import_projects

    Project.create(
        :order_number => crm_projects[0][0],
        :client_name => crm_projects[0][2],
        :partner_name => crm_projects[0][1],
        :totals_sales_orders => crm_projects[0][3],
        :actuals_cost => crm_projects[0][4],
        :actuals_sales => crm_projects[0][5],
        :external_cost => crm_projects[0][7],
        :external_sales => crm_projects[0][8],
        :total_change_request => crm_projects[0][6],
        :storage_path_sales_order => crm_projects[0][9],
        :storage_path_pm => crm_projects[0][10]
    )

#Project.create(:actuals_cost => 100) --> works :(
  end

The data is pulled from a SQLServer, thats why I work with the square brackets.

EDIT:

Alright, found the problem. Like I told before. In rake I make a connection with a remote database (SQLServer) and I want to store the data on a local database (PostgreSQL).

The problem was with the connection. When I pulled the data from the SQLServer, you need to switch from database and this is how you can do it.

ActiveRecord::Base.establish_connection :[one_of_databasename_in_your_yaml]
Kevin Gorjan
  • 1,272
  • 1
  • 17
  • 34
  • I thnk we need more details about how Project is defined, specifically about the actuals_cost field. You are initializing the environment, so that is good. It sounds like under rake, you are either getting some different set of gems, or a different database connection, or perhaps no database connection, ... – mcr Mar 26 '12 at 11:22
  • I'm using a PostgreSQL database. I receive data from crm_projects when I "puts" it. But when I want to store it, i'cant. only when I do it manually like this => Project.create(:actuals_cost => 100) – Kevin Gorjan Mar 26 '12 at 11:26
  • Oyeah, in the rake file, I make a connection with an external Database (SQLServer) and for saving the new data, I use a localhost PostgreSQL database. – Kevin Gorjan Mar 26 '12 at 11:46
  • Alright, found the problem. Like I told before. In rake I make a connection with a remote database (SQLServer) and I want to store the data on a local database (PostgreSQL). The problem was with the connection. When I pulled the data from the SQLServer, you need to switch from database and this is how you can do it. ActiveRecord::Base.establish_connection :[one_of_databasename_in_your_yaml] – Kevin Gorjan Mar 26 '12 at 12:16

1 Answers1

1

You can maintain multiple connections objects. If you are using ActiveRecord for both database connections, you can specify which connection to use in each model.

See: Multiple database connection in Rails and How to best handle per-Model database connections with ActiveRecord?

Community
  • 1
  • 1
mcr
  • 4,615
  • 2
  • 31
  • 30