在表

时间:2016-07-14 10:06:12

标签: ruby-on-rails database ruby-on-rails-4

如果不存在,我想创建一个新对象;或者只是更新其电子邮件(如果它已存在于我的数据库中) 我从CSV文件导入数据。

我尝试使用find_or_create_by方法,但仍然存在数据库中的对象。它不只是更新它。

Person.rb

 class Person < ActiveRecord::Base
   validates :email,  uniqueness: true
   require 'csv'

   def self.import_data
     filename = File.join Rails.root, '/vendor/people.csv'

     CSV.foreach(filename, headers: true, col_sep: ',') do  |row|
       firstname, lastname, home_phone_number, mobile_phone_number,   email, address = row
       Person.find_or_create_by(firstname: row['firstname'], lastname: row['lastname'], home_phone_number: row['home_phone_number'], mobile_phone_number: row['mobile_phone_number'], email: row['email'], address: row['address'])
     end
   end
 end

schema.rb

   create_table "people", force: :cascade do |t|
t.string   "email"
t.string   "home_phone_number"
t.string   "mobile_phone_number"
t.string   "firstname"
t.string   "lastname"
t.string   "address"
t.datetime "created_at",          null: false
t.datetime "updated_at",          null: false
end

4 个答案:

答案 0 :(得分:5)

这是解决方案:

person = Person.find_or_create_by(firstname: row["firstname"])
person.update_attributes({firstname: row['firstname'], lastname: row['lastname'], home_phone_number: row['home_phone_number'], mobile_phone_number: row['mobile_phone_number'], address: row['address'], email: row['email']})

我无法解释为什么这个解决方案有效,而不是你向我提出的解决方案。 但感谢@sohail_khalil和@abhilash的帮助!

答案 1 :(得分:2)

你需要这样做

Person.find_or_create_by(email: row['email']) do |person|
  person.update_attributes(firstname: row['firstname'], lastname: row['lastname'], home_phone_number: row['home_phone_number'], mobile_phone_number: row['mobile_phone_number'], address: row['address'])
end

你做错了是你在多列上应用find_or_create_by。只适用于一个。

我希望这会对您有所帮助并解决您的问题。

答案 2 :(得分:1)

添加@sohail所说的内容。为了使用多个参数来查找记录并随后更新或创建,您可以使用

Person.where(<conditions>).first_or_initialize do |person|
   <initialize code>
end

应该这样做。

答案 3 :(得分:-1)

如果你有Rails 3.2或更高版本,那么替换:

=ERROR REPORT==== 25-Nov-2016::18:42:45 ===
** Generic server <0.23222.460> terminating
** Last message in was {'$gen_cast',init}
** When Server state == {state,undefined,undefined,undefined,undefined,
                            {<<"/myvhost">>,<<"Test">>},
                            dynamic,
                            {shovel,
                                {endpoint,
                                    ["amqp://user:password@localhost:5672/myvhost"],
                                    #Fun<rabbit_shovel_parameters.4.22841904>},
                                {endpoint,
                                    ["amqp://user:password0@XXX.XXX.XXX.XXX:5672/myvhost"],
                                    #Fun<rabbit_shovel_parameters.5.22841904>},
                                1000,no_ack,
                                #Fun<rabbit_shovel_parameters.6.22841904>,
                                #Fun<rabbit_shovel_parameters.7.22841904>,
                                <<"queue_name">>,
                                1,'queue-length'},
                            undefined,undefined,undefined,undefined,undefined}
** Reason for termination ==
** {{badmatch,{error,not_allowed}},
    [{rabbit_shovel_worker,make_conn_and_chan,1,
                           [{file,"src/rabbit_shovel_worker.erl"},{line,236}]},
     {rabbit_shovel_worker,handle_cast,2,
                           [{file,"src/rabbit_shovel_worker.erl"},{line,62}]},
     {gen_server2,handle_msg,2,[{file,"src/gen_server2.erl"},{line,1049}]},
     {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,239}]}]}

使用:

 Person.find_or_create_by(firstname: row['firstname'], lastname: row['lastname'], home_phone_number: row['home_phone_number'], mobile_phone_number: row['mobile_phone_number'], email: row['email'], address: row['address'])