2013-01-31 48 views
0

陣列我創建具有以下模式中的表中的代碼插入使用續集寶石中的PostgreSQL

DB.create_table :Pokemon do 
    primary_key :id 
    String :first_name 
    String :last_name 
    String :email 
    String :zipcode 
    String :company_name 
    String :google_profile 
    String :skype 
    String :phone 
    String :about 
    String :linkedin_profile_url 
    String :company_url 
    column :needs , 'Text[]' 
    column :offering , 'Text[]' 
end 

爲需要和提供我正在插入一個字符串數組與下面的代碼

pokes=DB[:Pokemon]; 
off=['hello1' , 'hello2'] 
nee= ['need1' , 'need2'] 
pokes.insert(:first_name => 'abcd' ,:last_name => 'mehandiratta', :offering => off , :needs => nee) 

當我運行它我得到一個錯誤

PG::Error: ERROR: column "offering" is of type text[] but expression is of type record (Sequel::DatabaseError) 
LINE 1: ...fering", "needs") VALUES ('abcd', 'mehandiratta', ('hello1',... 
                  ^
HINT: You will need to rewrite or cast the expression. 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:145:in `execute_query' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/database/logging.rb:33:in `log_yield' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:145:in `execute_query' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:132:in `execute' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:111:in `check_disconnect_errors' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:132:in `execute' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:413:in `_execute' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:242:in `execute' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:425:in `check_database_errors' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:242:in `execute' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/database/connecting.rb:236:in `synchronize' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/connection_pool/threaded.rb:104:in `hold' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/database/connecting.rb:236:in `synchronize' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:242:in `execute' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/dataset/actions.rb:801:in `execute' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:525:in `fetch_rows' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/dataset/actions.rb:860:in `returning_fetch_rows' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/dataset/actions.rb:341:in `insert' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/shared/postgres.rb:1060:in `insert' 
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/shared/postgres.rb:1069:in `insert' 
    from hello.rb:10 

可以任何一個請告訴一個合適的方式如何在SERT陣列在續集寶石以及如何添加文本陣列中的列

回答

0

由於錯誤說,只投數組表達式爲字符串,像這樣:

pokes.insert(:first_name => 'abcd' ,:last_name => 'mehandiratta', :offering => off.to_s , :needs => nee.to_s)